Page 1 of 1

If Else go to new page....

Posted: Tue Apr 22, 2008 9:28 am
by latoyale
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Good Day,

I have a form. If users enter in the wrong password, I would like an error message to display and the user to be directed to my signin page. Unfortunately my if else statement doesn't work. It produces errors. My code is:

Code: Select all

 
if($result!= $myusername || $result!= $mypassword){
echo "Wrong Username or Password";
header("location:signup.php");}
 
else {// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
?>
The error I receive is:

Wrong Username or Password
Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/a/b/jaberkjaber/html/askslicky/checklogin.php:26) in /home/content/j/a/b/jaberkjaber/html/askslicky/checklogin.php on line 27


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: If Else go to new page....

Posted: Tue Apr 22, 2008 9:37 am
by Li0rE
The issue here has absolutely nothing to do with your if statement :)

Actually, to know why this isn't working you have to understand what the header() function does. It is giving the information to the browser about the page before the page is retrieved from the server.

This means that if you send information, and then send the header, it will not work. That is the error you got.

Now, here are two solutions:

Code: Select all

 
<?php
if($result!= $myusername || $result!= $mypassword){
//Eliminate the text here, and it will successfully redirect, in fact you could also place a get value on the url to say invalid password on the signup page (see bold)
header("location:signup.php[b]?error=1[/b]");}
 
else {// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
?>
Or you could keep the text there, and you could use a javascript redirect which waits for a couple seconds so the user can read the text, and then sends the user to the next page.