If Else go to new page....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

If Else go to new page....

Post 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.
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

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

Post 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.
Post Reply