connor.p
connor.p

Reputation: 876

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

Upvotes: 0

Views: 275

Answers (5)

Your Common Sense
Your Common Sense

Reputation: 158005

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>

Upvotes: 3

Michael
Michael

Reputation: 1231

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!

Upvotes: 0

random_user
random_user

Reputation: 848

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).

Upvotes: 0

Rylab
Rylab

Reputation: 1295

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;
}

Upvotes: 0

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

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' ) ;

Upvotes: 5

Related Questions