Jump to content

How to redirect a page after logging in?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
hudbarnett

hudbarnett

    Newbie

  • Members
  • PipPip
  • 23 posts
Hi all

Can anyone please help me. I have the code below that provides text saying "You've been successfully logged in" but i would like this to redirect me to another secure page

Can someone please help me with the redirect code for my script.

Thank you


<?php

session_start();


if (!empty($_POST[username]))

{

    require_once("connect.php");


    

    $query = mysql_query("SELECT * FROM members

                            WHERE username = '$_POST[username]'

                            AND password = '$_POST[password]'")

    or die ("Error - Couldn't login user.");

    

    $row = mysql_fetch_array($query)

    or die ("Error - Couldn't login user.");

    

    if (!empty($row[username])) 

    {

        $_SESSION[username] = $row[username];

        echo "Welcome $_POST[username]! You've been successfully logged in.";

    

        exit();

    }

    else // 

    {

        echo "Error - Couldn't login user.<br /><br />

            Please try again.";

        exit();

    }

}


?>



:confused:

Edited by Roger, 10 September 2010 - 08:05 AM.


#2
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
if (!empty($_POST[username]))
{
    require_once("connect.php");

    
    $query = mysql_query("SELECT * FROM members
                            WHERE username = '$_POST[username]'
                            AND password = '$_POST[password]'")
    or die ("Error - Couldn't login user.");
    
    $row = mysql_fetch_array($query)
    or die ("Error - Couldn't login user.");
    
    if (!empty($row[username])) 
    {
        $_SESSION[username] = $row[username];
        echo "Welcome $_POST[username]! You've been successfully logged in.";
[COLOR="Red"]sleep(5);
header("Location:filename.php");[/COLOR]
    
        exit();
    }
    else // 
    {
        echo "Error - Couldn't login user.<br /><br />
            Please try again.";
        exit();
    }
}

?>

Just change the red code to match where you want to point them, and how long you would like to have the message displayed and that should do it ;)

#3
hudbarnett

hudbarnett

    Newbie

  • Members
  • PipPip
  • 23 posts
Again,,upi are a star

Thank you very much for helping me out with my project. :rolleyes:

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
That is horribly inefficient, change the red code to this:
echo '<meta http-equiv="refresh" content="5;url=http://site.com/index.php">';
"5" being seconds to wait to redirect.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
glambert

glambert

    Newbie

  • Members
  • Pip
  • 3 posts
Personally, I take full advantage of $_SESSION and I would do something like:

<?php

session_start();

if(is_array($_SESSION['msg'])){
   foreach($_SESSION['msg'] as $msg){
      print("<div class=\"msg\">{$msg}</div>");
   }
}
unset($_SESSION['msg']);

session_write_close();
?>

for outputting any messages for the end user, or maybe switch to using AJAX if you want to get more technical? printing messages on a plain screen and then doing a redirect is very old-fashioned.