Closed Thread
Results 1 to 5 of 5

Thread: How to redirect a page after logging in?

  1. #1
    hudbarnett is offline Newbie
    Join Date
    Jan 2010
    Posts
    23
    Rep Power
    0

    How to redirect a page after logging in?

    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

    Code:
    <?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();
        }
    }
    
    ?>
    Last edited by Roger; 09-10-2010 at 09:05 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    DEViANT's Avatar
    DEViANT is offline Programming Expert
    Join Date
    Aug 2010
    Location
    South Africa
    Posts
    359
    Blog Entries
    4
    Rep Power
    8

    Re: Redirect after a login

    Code:
    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.";
    sleep(5);
    header("Location:filename.php");
        
            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

  4. #3
    hudbarnett is offline Newbie
    Join Date
    Jan 2010
    Posts
    23
    Rep Power
    0

    Re: Redirect after a login

    Again,,upi are a star

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

  5. #4
    Join Date
    Jun 2010
    Location
    Vancouver, Eh.
    Posts
    4,027
    Blog Entries
    7
    Rep Power
    39

    Re: Redirect after a login

    That is horribly inefficient, change the red code to this:
    Code:
    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 10,000 different steps.
    A textual description can be only part of your question, be sure to provide sample results, errors and your platform in the appropriate forums while asking.

  6. #5
    glambert is offline Newbie
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Re: Redirect after a login

    Personally, I take full advantage of $_SESSION and I would do something like:

    Code:
    <?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.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 09-15-2010, 03:15 PM
  2. Page Redirect after Error Message is Output to the Browser
    By makamo66 in forum PHP Development
    Replies: 9
    Last Post: 05-21-2010, 04:08 AM
  3. Replies: 0
    Last Post: 03-19-2010, 08:23 PM
  4. logging username
    By dandoe in forum ionFiles
    Replies: 1
    Last Post: 06-29-2009, 07:07 PM
  5. Logging ?
    By Victor in forum Java Help
    Replies: 5
    Last Post: 07-22-2008, 01:30 PM

Tags for this Thread

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