Wednesday, February 10, 2010

How to make a php fom that goes to another page if form info is true?

Ok...so I have my form set up and I can make it so if the form info is true, in this case the username and password exist, it displays text that says Access Granted. But, how do I make it so if the login was complete, the browser will link to another page? Like a login succesful page.How to make a php fom that goes to another page if form info is true?
When you identified the form info as TRUE as you say, you can't redirect to another page if you echo/print output (you'll get the ';headers sent..'; error) which you seem to already know, so my solution would be to do a page, example ';logging_in.php'; and you send headers to that, like header(';location: logging_in.php?user=bla';); and the logging_in.php would look like (excluding the unnecassary html) :





%26lt;head%26gt;


%26lt;meta http-equiv=';refresh'; content=';4'; url=page_they_will_fall_on_after.php';%26gt;%26lt;/鈥?br>

%26lt;body%26gt;


%26lt;p%26gt;Thanks for logging in, %26lt;?php $_GET['user'] ?%26gt;
You will now be redirected. If you wish not to wait, you can %26lt;a href=';page_they_will_fall_on_after.php';%26gt;鈥?here%26lt;/a%26gt;%26lt;/p%26gt;


%26lt;/body%26gt;





Now, in the META tag that will redirect, the value 4 is the number of seconds, you can modify that. I fetched the username via GET and included no addslashes() or whatever method you use to avoid hacks, that was to only point out the method. You could also use the session vars to output the username, also considering the fact that if the info was true, you're at the step of setting session vars. Hope this answered your question.





edit: To the one below me, your second example, you wouldn't see the text because headers to redirect are sent, therefore the text would be ignored.How to make a php fom that goes to another page if form info is true?
Just put your code before the %26lt;HTML%26gt; tag, it will still print/echo the text on the page but you can also still redirect the user





%26lt;?php if($exist==';true';){ header(';Location: http;//www.yourpage.com/success.php';); }else{ echo ';That username and password doesnt exist!';;} ?%26gt;


%26lt;html%26gt;


%26lt;head%26gt;


%26lt;title%26gt;your page%26lt;/title%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;





%26lt;/body%26gt;


%26lt;/html%26gt;





or you could use the same page to display a message if its true and a different message if its not true by passing variables in the url such as





%26lt;?php if($exist==';true';){ header(';Location: www.yourpage.com/yourpage.php?variable=t鈥?}?%26gt;


%26lt;html%26gt;


%26lt;head%26gt;


%26lt;?php $string = $_GET['exist']; ?%26gt;


%26lt;title%26gt;your page%26lt;/title%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;?php if($exist==';true';){ echo ';Put your success text here!';; }else{ echo ';Put your failure text here...';; } ?%26gt;


%26lt;/body%26gt;

No comments:

Post a Comment