Php header not redirecting

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

Livengood
Forum Newbie
Posts: 11
Joined: Tue Nov 01, 2011 2:48 pm

Php header not redirecting

Post 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??
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Php header not redirecting

Post by Celauran »

You've given us almost nothing to work with. Do you get any errors? Can you post the code you're using?
Livengood
Forum Newbie
Posts: 11
Joined: Tue Nov 01, 2011 2:48 pm

Re: Php header not redirecting

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Php header not redirecting

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Livengood
Forum Newbie
Posts: 11
Joined: Tue Nov 01, 2011 2:48 pm

Re: Php header not redirecting

Post by Livengood »

Yes they are all met and the data is put into database, but the redirect isn't going.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Php header not redirecting

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Livengood
Forum Newbie
Posts: 11
Joined: Tue Nov 01, 2011 2:48 pm

Re: Php header not redirecting

Post 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
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

Re: Php header not redirecting

Post 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
?>
Livengood
Forum Newbie
Posts: 11
Joined: Tue Nov 01, 2011 2:48 pm

Re: Php header not redirecting

Post 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
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

Re: Php header not redirecting

Post 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.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Php header not redirecting

Post 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.
Warning: I have no idea what I'm talking about.
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

Re: Php header not redirecting

Post 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.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Php header not redirecting

Post 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
Last edited by thinsoldier on Fri Nov 25, 2011 10:05 pm, edited 1 time in total.
Warning: I have no idea what I'm talking about.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Php header not redirecting

Post 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.
Warning: I have no idea what I'm talking about.
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

Re: Php header not redirecting

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