Page 1 of 2

Php header not redirecting

Posted: Thu Nov 24, 2011 2:02 pm
by Livengood
I have my registration script for my site, if theirs a error in the submitted data it will send them to a certain error link, but the header doesnt send. How can i fix this issues??

Re: Php header not redirecting

Posted: Thu Nov 24, 2011 2:21 pm
by Celauran
You've given us almost nothing to work with. Do you get any errors? Can you post the code you're using?

Re: Php header not redirecting

Posted: Thu Nov 24, 2011 2:39 pm
by Livengood

Code: Select all

} elseif ($_GET['action'] == 'register'){
			//Register
			$firstname = $_POST["reg_first_name"];
			$lastname = $_POST["reg_last_name"];
			$email = $_POST["reg_email"];
			$password = md5($_POST["reg_pass"]);
			$sex = $_POST["sex"];
			$birthday = $_POST["bir_month"] . "/" . $_POST["bir_day"] . "/" . $_POST["bir_year"];
			
			$findat = strpos($email, '@');
			
			if(!$_POST["reg_first_name"]){
				header('Location: /index.php?reg=error1');
				//redirectme("/index.php?reg=error1");
			} elseif(!$_POST["reg_last_name"]){
				header('Location: /index.php?reg=error2');
			} elseif(!$_POST["reg_email"]){
				header('Location: /index.php?reg=error3');
			} elseif(!$_POST["reg_pass"]){
				header('Location: /index.php?reg=error4');
			} elseif(($_POST["bir_month"] == '-1') || ($_POST["bir_day"] == '-1') || ($_POST["bir_year"] == '-1')){
				header('Location: /index.php?reg=error5');
			} elseif(($_POST["reg_sex"] == '-1')){
				header('Location: /index.php?reg=error10');
			} elseif(!$findat){
				header('Location: /index.php?reg=error6');
			} else {
				if($_POST["re_email"] == $email){
					if(md5($_POST["re_pass"]) == $password){
						db();
						
						//Checks if email is taken
						$result=mysql_query("SELECT * FROM users WHERE email='". $email ."'");
						$count=mysql_num_rows($result);
						
						if($count > 0){
							header('Location: /index.php?reg=userex');
						} else {
							mysql_query("INSERT INTO users (fname, lname, email, pword, birthday, sex, rank) VALUES ('{$firstname}', '{$lastname}', '{$email}', '{$password}', '{$birthday}', '{$sex}', 'user')");
							
							$check = mysql_insert_id();
							$result = mysql_query("SELECT * FROM users WHERE id='{$check}'");
							
							$count=mysql_num_rows($result);
							
							if($count==1){
								mysql_close();
								header('Location: /index.php?reg=success');
							} else {
								mysql_close();
								header('Location: /index.php?reg=error9');
							}
						}
					} else {header('Location: /index.php?reg=error8');}
				} else {header('Location: /index.php?reg=error7');}
			}
			//End Register
This is the section of the code i am using, but the header doesn't redirect it like skips over it.

Re: Php header not redirecting

Posted: Thu Nov 24, 2011 3:33 pm
by pickle
Are the if() conditions met?

You should also call exit(); after a redirect header, as header() doesn't stop execution of the script.

Re: Php header not redirecting

Posted: Thu Nov 24, 2011 5:06 pm
by Livengood
Yes they are all met and the data is put into database, but the redirect isn't going.

Re: Php header not redirecting

Posted: Thu Nov 24, 2011 5:14 pm
by pickle
Maybe the format of your header is wrong? Technically it's supposed to have a fully qualified url, not just a relative url.

Re: Php header not redirecting

Posted: Thu Nov 24, 2011 5:52 pm
by Livengood
its been working before i added this line:

} elseif(($_POST["reg_sex"] == '-1')){
header('Location: /index.php?reg=error10');

i will try the full link now

Re: Php header not redirecting

Posted: Fri Nov 25, 2011 3:29 am
by Hermit TL
I was doing something very similar to what you describe. I don't remember why I eventually stopped using the header redirect method, I know I had a reason. This probably is not ideal (since it's javascript) but I have been using this for redirecting for a while and it works fine for me.

Code: Select all

<?php
//******************************************************************
//******************************************************************
// FUNCTION
//
// v0.1a java_send by Hermit TL
//
// Description: Function for HTML embedded javascript URL redirect.
//
// Details: This function echos a javascript url redirect to the
//		html code which is executed by the browser
//		when read.
//
// Function Instructions:
// -$url: Desired url redirect
// -useage ie: --java_send($url);
//		-java_send("somepage.php");
//		-java_send("http://somepage.php");
//
//******************************************************************
//******************************************************************

//START java_send function
	function java_send($url){
		echo "<font color=lightblue>Click <a href=\"$url\">here</a> if you are not automatically redirected.<br />Enable Javascript to avoid seeing this message.</font>";
		echo '<script>location.href="'
		 . $url
		 . '"</script>';
	}
//END java_send function
?>

Re: Php header not redirecting

Posted: Fri Nov 25, 2011 3:16 pm
by Livengood
the thing is ive tried this lol i just need a way so i can redirect back so i can get errors, or display message

Re: Php header not redirecting

Posted: Fri Nov 25, 2011 3:35 pm
by Hermit TL
I put my error codes in $_SESSION['error'] and run it through a custom made error checking include script designed for my site.

Re: Php header not redirecting

Posted: Fri Nov 25, 2011 9:27 pm
by thinsoldier
Do you have error reporting turned on?

The most common reason for a header redirect to not send it because you've already echoed content to the browsers at some prior point in the code and would get the error "headers already sent". If you're just getting a blank page instead of a redirect I'd guess you're getting that fatal error but don't see it due to error reporting being turned off.

Personally I'd have each if statement define a variable then at the end of my script just use a single header() call with the variable.

header('Location: /index.php?reg=error6 The number at the end seems to be the only difference between all the header calls.

Re: Php header not redirecting

Posted: Fri Nov 25, 2011 9:56 pm
by Hermit TL
The most common reason for a header redirect to not send it because you've already echoed content to the browsers at some prior point in the code and would get the error "headers already sent".
YES! Now I remember. That's exactly why I stopped using it. Thank you.

Re: Php header not redirecting

Posted: Fri Nov 25, 2011 10:00 pm
by thinsoldier
In my style

Code: Select all

if(whatever){...} 
elseif ($_GET['action'] == 'register')
{
    //Register
    $firstname = $_POST["reg_first_name"];
    $lastname = $_POST["reg_last_name"];
    $email = $_POST["reg_email"];
    $password = md5($_POST["reg_pass"]);
    $sex = $_POST["sex"];
    $birthday = $_POST["bir_month"] . "/" . $_POST["bir_day"] . "/" . $_POST["bir_year"];
   
    $findat = strpos($email, '@');
   
       //--------------------------------------------    
    // check for errors and set error code
    //--------------------------------------------

    $errornum = null;
    if(!$_POST["reg_first_name"]){ $errornum = 1; }
    if(!$_POST["reg_last_name"]){ $errornum = 2; }
    if(!$_POST["reg_email"]){ $errornum = 3; }
    if(!$_POST["reg_pass"]){ $errornum = 4; }
    if(($_POST["bir_month"] == '-1') || ($_POST["bir_day"] == '-1') || ($_POST["bir_year"] == '-1'))
    {$errornum = 5; }
    if(($_POST["reg_sex"] == '-1')){ $errornum = 10; }
    if(!$findat){$errornum = 6; }
    if($_POST["re_email"] == $email){ $errornum = 7; }
    if(md5($_POST["re_pass"]) == $password){ $errornum = 8; }

    //--------------------------------------------    
    // redirect with error code and exit if there is an error
    //--------------------------------------------
    
    if( $errornum ) { header('Location: /index.php?reg=error'.$errornum); exit; }
    
    //--------------------------------------------
    // if there were no errors then continue below
    //--------------------------------------------
    
    db();
    //Checks if email is taken
    $result=mysql_query("SELECT * FROM users WHERE email='". $email ."'");
    $count=mysql_num_rows($result);
   
    if($count > 0){ header('Location: /index.php?reg=userex'); } 
    else
    {
        mysql_query("INSERT INTO users (fname, lname, email, pword, birthday, sex, rank) VALUES ('{$firstname}', '{$lastname}', '{$email}', '{$password}', '{$birthday}', '{$sex}', 'user')");
       
        $check = mysql_insert_id();
        $result = mysql_query("SELECT * FROM users WHERE id='{$check}'");
       
        $count=mysql_num_rows($result);
       
        if($count==1){
            mysql_close();
            header('Location: /index.php?reg=success');
        } else {
            mysql_close();
            header('Location: /index.php?reg=error9');
        }
    }
} //End Register

Re: Php header not redirecting

Posted: Fri Nov 25, 2011 10:04 pm
by thinsoldier
Hermit TL wrote:
The most common reason for a header redirect to not send it because you've already echoed content to the browsers at some prior point in the code and would get the error "headers already sent".
YES! Now I remember. That's exactly why I stopped using it. Thank you.
I've found that by leaving the html output to the very last after I've done my most important logic is more flexible and never gets in the way of using header();

Doing it that way by the time I get to any logic relevant to showing/hiding parts of the html I'd have already been long finished with any logic important enough to trigger a redirect.

Re: Php header not redirecting

Posted: Fri Nov 25, 2011 10:34 pm
by Hermit TL
I've found that by leaving the html output to the very last after I've done my most important logic is more flexible and never gets in the way of using header();
The reason I ended up getting rid of it was after I started using an index.php file full of logic and include files to load the rest of the site and had css and html loading in the index I started getting the error.