Results 1 to 7 of 7

Thread: How to redirect from one page to another

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    How to redirect from one page to another

    Hi. I'm new to php development and i have to work on Redirecting a page. As an example i create a page with 2 textboxes and a button. If I enter admin admin as username and password so it gives message that you logged in successfully.

    What i want now that when this message appears so after this the page redirects to assignment2.php. For this purpose i have the following code:

    Code:
    <form method="POST" action="assignment2.php">
    
        <input type="text" name="uname" /><br />
        <input type="password" name="pwd" />
        
        <input type="submit" value="Check Me" name="set" />
    
    </form>
    
    <?php
    
    if($_POST['set'])
        {
            
        if($_POST['uname'] == "admin" &&  $_POST['pwd'] == "admin")
        {
            echo "You logged in successfully";
        }
    
        else
        {
            echo "Your User Name or Password is not matching ";
        }
        
        }
    
    ?>
    and this is the code of assignment2.php

    Code:
    <?php header("Location: assignment2.php"); ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <h3> your are redirected here to assignment2.php page </h3>
    </body>
    </html>
    I know that there might be mistakes, but its my learning stage so please guide me for proper way for redirecting a page. Thanks.

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: How to redirect from one page to another

    A good rule of thumb for web development (specifically php since thats what I work with) is you want to remember that pages are executed from top line to bottom line.

    Put all of your PHP code at the top of your document so that will be executed first.

    When you successfully login you want to use header("Location: assignment2.php "); not on the actual page. If you have if you have a location header on the page its redirecting to, it will cause an infinite loop and the browser will stop loading.

    PHP Code:
    <?php

    if($_POST['set'])
        {
            
        if(
    $_POST['uname'] == "admin" &&  $_POST['pwd'] == "admin")
        {
            
    header("Location: assignment2.php");
        }

        else
        {
            echo 
    "Your User Name or Password is not matching ";
        }
        
        }

    ?>
    <form method="POST" action="assignment2.php">

        <input type="text" name="uname" /><br />
        <input type="password" name="pwd" />
        
        <input type="submit" value="Check Me" name="set" />

    </form>

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to redirect from one page to another

    Very well dclamp.

    Now, its true that execution starts from top to bottom. But in my code when the php code will be executed first, so i want that if the username and pwd are not admin admin so redirection must not be possible. I want that if the username and pwd matches then a message appears that "You logged in successfully" and then the page redirects to assignment2.php and if the condition is not true then it gives message that "Your username or pwd does not match".

    Question.
    How do I apply this condition to restrain the browser to not to redirect if the login is not successful?


    Secondly, I removed the header function from the assignment2.php the control still goes to assignment2.php.

    Question2.
    I'm not using any redirect or header function then how page is being redirected to assignment2.php?

    Thanks.

  4. #4
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Re: How to redirect from one page to another

    hello...
    question 1 : just make an if statement for it!
    the 2nd part is because of <form method="POST" action="assignment2.php"> it says lets post the variables to our action that is assignment2.php thats why it still redirect it

    i guess ur question 2 is answered as well
    btw i had some problem with the header redirect so i started using <meta http-equiv="refresh" content="10; ,URL=http://www.mysite.com"> instead
    is just as simple as it is but u have to use an if statement !
    Last edited by Pc Monk; Jan 23rd, 2015 at 09:47 AM.
    Body Language tells the truth! even from the grave tsaeb eht morf gninnur ,nwod deaH
    All the big things started from little! teef my tsap evom sekans ,duol raor slluB
    Lietome.ir

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: How to redirect from one page to another

    it ends up looking something like this:

    php Code:
    1. <?php
    2.  
    3. $showLogin = 1; // being pessimistic, by default, let's show the form
    4.  
    5. if($_POST['set'])
    6. {
    7.        
    8.     if($_POST['uname'] == "admin" &&  $_POST['pwd'] == "admin")
    9.     {
    10.  
    11.  
    12. // NOTE - the key though is making sure this next line ENDS UP IN THE RIGHT PLACE of the rendered page... it's not going to work if it's burried in the body... it needs to be in the head section.
    13. echo('<meta http-equiv="refresh" content="10; ,URL=http://www.mysite.com">');
    14.  
    15. // continue the echos to display the you're logged in text, or better yet, put it in a file and just include it
    16. // include('loginsuccessful.php'); // NOTE: this included file could also easily include some javascript code that will fire off a timer and then redirect the user then you wouldn't need the meta refresh tag above. But it does rely on the client having JS running.
    17. $showLogin = 0; // Ok, now we've verified we don't need to show the login form again...
    18.     }
    19.  
    20.     else
    21.     {
    22.         echo "Your User Name or Password is not matching ";
    23. $showLogin = 1; // flip the bit we know later to re-display the login form
    24.     }
    25. }
    26.  
    27. if ($showLogin == 1) {
    28. // here we display the form
    29. echo('<form method="POST" action="assignment2.php">');
    30. echo('    <input type="text" name="uname" /><br />');
    31. echo('    <input type="password" name="pwd" />');
    32. echo('    <input type="submit" value="Check Me" name="set" />');
    33. echo('</form>');
    34. }
    35. ?>

    In reality, I'd actually segment that all off, put things into various files and include them at will ... I'd also probably partition stuff off in to functions, since odds are there's more to the page than the simple login shown here.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to redirect from one page to another

    @pc Monk, @tg

    Thanks a lot to both of you.
    This seems to me a bit difficult because my level of php is not as advance as you. I just learned so far that how to use the header function to redirect the browser to another page. I studied the meta http_...... but didn't understand that.

  7. #7
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Re: How to redirect from one page to another

    idk if its too late or not but ill explain:
    think this as an string : <meta http-equiv="refresh" content="10; ,URL=http://www.mysite.com/assignment2.php />
    now in your code ill place it
    Code:
    <?php
    
    if($_POST['set'])
        {
            
        if($_POST['uname'] == "admin" &&  $_POST['pwd'] == "admin")
        {
            echo '<meta http-equiv="refresh" content="10; ,URL=http://www.mysite.com/assignment2.php />';
        }
    
        else
        {
            echo "Your User Name or Password is not matching ";
        }
        
        }
    
    ?>
    <form method="POST" action="assignment2.php">
    
        <input type="text" name="uname" /><br />
        <input type="password" name="pwd" />
        
        <input type="submit" value="Check Me" name="set" />
    
    </form>
    IF Statement = True > redirect
    != False > echo "Your User Name or Password is not matching ";
    what content means in the meta tag : its the time!10(s).. 10 seconds right ? RIGHT!
    hope its simple enough
    if its not then u dont understand the concept of IF STATEMENT that it can be explained too
    Body Language tells the truth! even from the grave tsaeb eht morf gninnur ,nwod deaH
    All the big things started from little! teef my tsap evom sekans ,duol raor slluB
    Lietome.ir

Posting Permissions

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



Click Here to Expand Forum to Full Width