Jump to content

Problem with header() function


ad5901

Recommended Posts

Am new to php and currently developing simple login site.  Information can be entered and input to the mySQL database and retrieved from the database.  However when I use the header function it does not appear to redirect to the required page.  All I see are blank pages which should contain information.  The URL in the browser doesn't change either.  Have tested in both safari and firefox browsers as using a mac. 

 

Have tried using an absolute URL as follows:

 

<?phpheader('Location: http://www.ab-productions.co.uk/tvts/thanks2.php');header("Content-Length: 0");?>

 

and also the $_SERVER superglobal - this page should redirect to loggedin.php:

<?php # Script 9.1 - login.php
// Send NOTHING to the Web browser prior to the setcookie() lines!

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

require_once ('../../mysql_connect.php'); // Connect to the db.
  
$errors = array(); // Initialize error array.

// Check for an email address.
if (empty($_POST['email'])) {
  $errors[] = 'You forgot to enter your email address.';
} else {
  $e = escape_data($_POST['email']);
}

// Check for a password.
if (empty($_POST['password'])) {
  $errors[] = 'You forgot to enter your password.';
} else {
  $p = escape_data($_POST['password']);
}

if (empty($errors)) { // If everything's OK.

  /* Retrieve the user_id and first_name for 
  that email/password combination. */
  $query = "SELECT user_id, first_name FROM production WHERE email='$e' AND password=SHA('$p')";  
  $result = @mysql_query ($query); // Run the query.
  $row = mysql_fetch_array ($result, MYSQL_NUM); // Return a record, if applicable.

  if ($row) { // A record was pulled from the database.
    
   // Set the cookies & redirect.
   setcookie ('user_id', $row[0]);
   setcookie ('first_name', $row[1]);

   // Redirect the user to the loggedin.php page.
   // Start defining the URL.
   $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
   // Check for a trailing slash.
   if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
    $url = substr ($url, 0, -1); // Chop off the slash.
   }
   // Add the page.
   $url .= '/loggedin.php';
   
   header("Location: $url");
   exit(); // Quit the script.
    
  } else { // No record matched the query.
   $errors[] = 'The email address and password entered do not match those on file.'; // Public message.
   $errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message.
  }
  
} // End of if (empty($errors)) IF.
  
mysql_close(); // Close the database connection.

} else { // Form has not been submitted.

$errors = NULL;

} // End of the main Submit conditional.

// Begin the page now.
$page_title = 'Login';
include ('./includes/header.html');

if (!empty($errors)) { // Print any error messages.
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
  echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}

// Create the form.
?>
<h2>Login</h2>
<form action="login.php" method="post">
<p>Email Address: <input type="text" name="email" size="20" maxlength="40" /> </p>
<p>Password: <input type="password" name="password" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Login" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('./includes/footer.html');
?>

 

Anyone any idea what I'm doing wrong??

[/]

Link to comment
https://forums.phpfreaks.com/topic/174783-problem-with-header-function/
Share on other sites

This has nothing todo with the installation/configuration off php...

 

If you get the error: headers are already sent by ...

The browser couldn't sent the headers because there already was outputted something like an echo command.

 

If you don't get it:

I think it should be:

header('Location: ' . $url);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.