-
Jan 22nd, 2015, 09:05 AM
#1
Thread Starter
Fanatic Member
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.
-
Jan 22nd, 2015, 04:29 PM
#2
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>
-
Jan 23rd, 2015, 12:04 AM
#3
Thread Starter
Fanatic Member
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.
Last edited by ADQUSIT; Jan 23rd, 2015 at 12:10 AM.
-
Jan 23rd, 2015, 09:44 AM
#4
Addicted Member
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
-
Jan 23rd, 2015, 10:41 AM
#5
Re: How to redirect from one page to another
it ends up looking something like this:
php Code:
<?php
$showLogin = 1; // being pessimistic, by default, let's show the form
if($_POST['set'])
{
if($_POST['uname'] == "admin" && $_POST['pwd'] == "admin")
{
// 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.
echo('<meta http-equiv="refresh" content="10; ,URL=http://www.mysite.com">');
// continue the echos to display the you're logged in text, or better yet, put it in a file and just include it
// 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.
$showLogin = 0; // Ok, now we've verified we don't need to show the login form again...
}
else
{
echo "Your User Name or Password is not matching ";
$showLogin = 1; // flip the bit we know later to re-display the login form
}
}
if ($showLogin == 1) {
// here we display the form
echo('<form method="POST" action="assignment2.php">');
echo(' <input type="text" name="uname" /><br />');
echo(' <input type="password" name="pwd" />');
echo(' <input type="submit" value="Check Me" name="set" />');
echo('</form>');
}
?>
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
-
Jan 26th, 2015, 10:52 AM
#6
Thread Starter
Fanatic Member
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.
-
Jan 29th, 2015, 04:11 PM
#7
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|