php redirection not working

Posted on

php redirection not working – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about php redirection not working.

Ive got this register script that puts the information into a mysql database. now it all works fine and when someone does something wrong its says the error (e.g. “Username not defined”)

but when it goes wrong it does not look very good because it just displays the message on an empty page, so i thought i would make it redirect to the form page and display the message there.

here is the working script

$forename = $_POST['forename'];      
$surname = $_POST['surname'];       
$email = $_POST['email'];    
$password = $_POST['password'];    
$username = $_POST['username'];

$errors = array();

 if(!$username) {

    $errors[] = "Username is not defined";

 }

 if(!$password) {

    $errors[] = "Password is not defined";

 }

and it continues.

now i just thought i could do this

 $errors = array();

 if(!$username) {

    $errors[] = header( 'Location: http://localhost/muiltabledistractions/#!/page_register_error-Username-is-not-defined' ) ;

 }

 if(!$password) {

    $errors[] = "Password is not defined";

 }

but no, all it does is ignore it.

could someone please help me

please feel free to ask for more of the script if you need it

many thanks connor

Solution :

You cannot wrap a header in a array like that.

You just call the function, then it redirects.

header( 'Location: http://localhost/muiltabledistractions/#!/page_register_error-Username-is-not-defined' ) ;

it does not look very good because it just displays the message on an empty page,

What’s the problem?
Why not to show the form again? with fields already filled.
This is going to be a user-friendly interface.

Just include your form in the same page with fields populated.
That’s more common way than your redirects to blank form.

This is called POST/Redirect/GET pattern and here goes a short example of it:

the code

<?  
if ($_SERVER['REQUEST_METHOD']=='POST') {  

  $err = array();
  //performing all validations and raising corresponding errors
  if (empty($_POST['name']) $err[] = "Username field is required";  
  if (empty($_POST['text']) $err[] = "Comments field is required";  

  if (!$err) {  
    // if no errors - saving data 
    // and then redirect:
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
  }  else {
    // all field values should be escaped according to HTML standard
    foreach ($_POST as $key => $val) {
      $form[$key] = htmlspecialchars($val);
    }
} else {
  $form['name'] = $form['comments'] = '';  
}
include 'form.tpl.php';
?>  

the template

<? if ($err): ?>
  <? foreach($err as $e): ?>
<div class="err"><?=$e?></div>
  <? endforeach ?>
<? endif ?>
<form>
  <input type="text" name="name" value="<?=$form['name']?>">
  <textarea name="comments"><?=$form['comments']?></textarea>
  <input type="submit">
</form>

You are placing the return value of the header function in an array, then continuing with your page execution.

If you don’t care about anything that would normally happen below that redirection, which I believe is what you’re implying, you should just set the header and then immediately exit. Do not try to place the return value of the header function into the errors array like that, as there’s no point.

if(!$username) {
   header('Location: http://localhost/muiltabledistractions/#!/page_register_error-Username-is-not-defined');
    exit;
}

I don’t if this is the problem, but it’s important to include the status code in header too. Like:

header("Location: /foo.php",TRUE,302);

307 for Temporary Redirect, 302 for permanently moved. Chrome, a while ago, didn’t accepted headers redirect without status code (i don’t know nowadays).

try this after filling your error array:

if (count($errors) > 0) 
{
  header( 'Location: http://localhost/muiltabledistractions/#!/page_register_error-Username-is-not-defined' );
  exit;
}

Keep in mind there should be no html output before this part!

Leave a Reply

Your email address will not be published. Required fields are marked *