Jump to content

PHP - Header('Location: xxx); isnt working


Go to solution Solved by requinix,

Recommended Posts

I dont understand why my header(Location) isnt working. Does any one see why?

 
<?php 
include 'core/init.php';
logged_in_redirect();
include 'includes/overall/header.php';
 
if (empty($_POST) === false) {
$required_fields = array('first_name', 'last_name', 'username', 'password', 'password_again', 'email');
foreach($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true) {
$errors[] = 'All field are required';
break 1;
}
}
 
if (empty($errors) === true) {
if (user_exists($_POST['username']) === true) {
$errors[] = 'Sorry, the user \'' . $_POST['username'] . '\' is already taken';
}
if (preg_match("/\\s/", $_POST['username']) == true) {
$errors[] = 'Your username can not have spaces';
 
}
if (strlen($_POST['password']) < 6) {
$errors[] = 'Your password must be at least 6 characters long';
}
 
if ($_POST['password'] !== $_POST['password_again']) {
$errors[] = 'Your passwords do not match';
}
 
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'A valid email address is required';
}
 
if (email_exists($_POST['email']) === true) {
$errors[] = 'Sorry, the email \'' . $_POST['email'] . '\' is already is use. Please contact your site adimn is this is incorrect.';
}
 
}
}
 
if (isset($_GET['success']) && empty($_GET['success'])) {
echo '<div class="container"><div class="background-success text-center"><div class="alert alert-success center-background">You\'ve be registered successfully! Please check your email to activate your account</div><div class="center-background"><div class="center-text-background">if any issues occur please send us an email!! support@bettergamerzunited.com</div></div></div>';
} else {
if(empty($_POST) === false && empty($errors) === true) {
$register_data = array(
'first_name'  => $_POST['first_name'],
'last_name'  => $_POST['last_name'],
'username'  => $_POST['username'],
'password'  => $_POST['password'],
'email'  => $_POST['email'],
'email_code'  => md5($_POST['username'] + microtime())
);
 
register_user($register_data);
header('Location: register.php?success');
exit();
}
 
?>
<div class="container background">
<form action="" method="POST" class="form-horizontal" role="form">
 <div class="form-group">
   <label for="inputfirstname3" class="col-sm-2 control-label">First Name</label>
   <div class="col-sm-10">
     <input type="text" name="first_name" class="form-control" id="inputfirstname3" value="" autocomplete="off" placeholder="First Name">
   </div>
 </div>
 <div class="form-group">
   <label for="inputlastname3" class="col-sm-2 control-label">Last Name</label>
   <div class="col-sm-10">
     <input type="text" name="last_name" class="form-control" id="inputlastname3" value="" autocomplete="off" placeholder="Last Name">
   </div>
 </div>
 <div class="form-group">
   <label for="inputusername3" class="col-sm-2 control-label">Username</label>
   <div class="col-sm-10">
     <input type="text" name="username" class="form-control" id="inputusername3" value="" autocomplete="off" placeholder="Username">
   </div>
 </div>
 <div class="form-group">
   <label for="inputemail3" class="col-sm-2 control-label">Email</label>
   <div class="col-sm-10">
     <input type="email" name="email" class="form-control" id="inputemail3" value="" autocomplete="off" placeholder="Email Address">
   </div>
 </div>
 <div class="form-group">
   <label for="inputpassword3" class="col-sm-2 control-label">Password</label>
   <div class="col-sm-10">
     <input type="password" name="password" class="form-control" id="inputpassword3"  autocomplete="off" placeholder="Password">
   </div>
 </div>
 <div class="form-group">
   <label for="inputpassword_again3" class="col-sm-2 control-label">Validate Password</label>
   <div class="col-sm-10">
     <input type="password" name="password_again" class="form-control" id="inputpassword_again3" autocomplete="off" placeholder="Validate Password">
   </div>
 </div>
 <div class="form-group">
   <div class="col-sm-10">
     <p class="info">Please be aware that this does not mean that you are a member of Better Gamerz United. If you would like to be come a member please
     contuine with the registration and then fill out our application. Other wise you will have limited use of this site. Thank you Team BGU!! </p>
   </div>
 </div>
 <div class="form-group">
   <div class="col-sm-offset-2 col-sm-10">
    <button type="submit" class="btn btn-primary pull-right register">Register</button>
   </div>
 </div>
</form>
</div>
 
<?php 
}
include 'includes/overall/footer.php'; ?>
 
Link to comment
https://forums.phpfreaks.com/topic/290831-php-headerlocation-xxx-isnt-working/
Share on other sites

  • Solution

Find your php.ini and set

error_reporting = -1
display_errors = on
restart your web server, and try your script again. You'll likely get errors to the tune of being unable to send headers because output started. I'm guessing on line 1. Can't use header() if there has been output.

Warning: Cannot modify header information - headers already sent by (output started at /home/afmccombs/public_html/includes/overall/header.php:3) in /home/afmccombs/public_html/register.php on line 56

Edited by afmccombs

Find your php.ini and set

error_reporting = -1
display_errors = on
restart your web server, and try your script again. You'll likely get errors to the tune of being unable to send headers because output started. I'm guessing on line 1. Can't use header() if there has been output.

 

Thank you,

 

I looked up a solution to my problem. Could you please explain what ob_start(); (thats what fixed my problem) is. I have never used it before. like dummy proof it please

Why do you use a function when you have no idea what it does?

 

ob_start() is not a solution. It's a workaround at best. The problem is that your header.php script already generates output while you're still doing application logic. That's bad. The output should come after you've processed the data, decided what to do, emitted Location headers and whatnot.

 

So this is a problem of your application structure. Fix it, and the error will go away.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.