SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Problems redirecting in php

    <?php
    header("location:main.php");
    ?>

    The script works on itself. It redirects to another page perfectly, but when i use it with other scripts it just doesn't. I've got an if statement which checks if username/password is correct.

    so if username is correct i want the user to be redirected to another page. Also can i introduce a small interval. Like a sleep(); function? I'm not sure if i can do it. So the user can see that the password is correct, then he/she gets redirected to another page.

    Why can't i use the above redirect header script inside my loop?

    Please help. Thank you.

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This will redirect the user after three seconds:

    PHP Code:
    header('Refresh: 3; URL="main.php"'); 
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    man, it doesn't work, dunno why...it says cannot use, header already sent..

    i'll give you the script i'm trying to do...

    <?php


    // connect to the mysql server
    $conn = @mysql_connect("localhost","user","psswd")
    or die ("Could not connect to mysql table");

    // select the database
    $rs=@mysql_select_db("database",$conn)
    or die ("Could not select database");

    //create query
    $sql="select * from tblUsers where username='$username' and password='$password'";

    $rs=mysql_query($sql,$conn) or die(mysql_error());

    if($rs && mysql_num_rows($rs))
    {
    echo "hello, ";
    echo "$username";
    header('Refresh: 3; URL="main.php"');
    }
    else
    {
    echo "Combination Username/Password is incorrect. Re-enter!";
    }

    ?>

    that doesn't work.. i also tried putting that header('Refresh: 3; URL="main.php"'); at the end, it didn't work either,

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You cannot have any output (HTML, text, spaces, newlines, etc) before sending a header.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I overcome the problem?

  6. #6
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Try using the output buffering functions:

    PHP Code:
    <?php

    ob_start
    ();

    // connect to the mysql server
    $conn = @mysql_connect("localhost","user","psswd")
    or die (
    "Could not connect to mysql table");

    // select the database
    $rs=@mysql_select_db("database",$conn)
    or die (
    "Could not select database");

    //create query
    $sql="select * from tblUsers where username='$username' and password='$password'";

    $rs=mysql_query($sql,$conn) or die(mysql_error());

    if(
    $rs && mysql_num_rows($rs))
    {
    echo 
    "hello, ";
    echo 
    "$username";
    header('Refresh: 3; URL="main.php"');
    }
    else
    {
    echo 
    "Combination Username/Password is incorrect. Re-enter!";
    }

    ob_end_flush();

    ?>
    I'm assuming that the opening tag is positioned at the top of the file.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry it took me so long to reply. Thanks for pointing out how it should be done. I've done it the way you told me to do the first time. I just took away the echo code in if stement, and now i link to another page where i display the very same msg.

    Thanks a lot man!

    the code i've used is below....

    <?php

    // connect to the mysql server
    $conn = @mysql_connect("localhost","user","psswd")
    or die ("Could not connect to mysql table");

    // select the database
    $rs=@mysql_select_db("database",$conn)
    or die ("Could not select database");

    //create query
    $sql="select * from tblUsers where username='$username' and password='$password'";

    $rs=mysql_query($sql,$conn) or die(mysql_error());

    if($rs && mysql_num_rows($rs))
    {
    header('Refresh: 3; URL="main.php"');
    }
    else
    {
    echo "Combination Username/Password is incorrect. Re-enter!";
    }

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •