Jump to content

redirct_to function help


1984321

Recommended Posts

Hi readers,

 

recently I have decided to upload my php/mysql website online and found an error that I can't resolve. Basically when I login/logout as an user it won't redirect me to the right web page instead it returns a blank page with the same URL , this problem only occur online but works fine in the localhost server. The login page is used to prevent unauthorised user from using the private webpage and not for customizing unique page for each user. 

 

The redirect_to function is in function.php

 

Redirect_to function:

function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}

 

Logout.php:

 


<?php require_once("includes/function.php"); ?>


<?php
session_start();

$_SESSION = array();

if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
      }
session_destroy();
redirect_to("login.php?logout=1");
?>

 

Login.php:

<?php

if(logged_in()) {
	redirect_to("staff.php");
} 
  
include_once("includes/form_functions.php");

if (isset($_POST['submit'])) { 
	$errors = array();

$required_fields = array('username', 'password');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));

$fields_with_lengths = array('username' => 15, 'password' => 20);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

$username = trim(mysql_prep($_POST['username']));
$password = trim(mysql_prep($_POST['password']));
$hashed_password = sha1($password);

if (empty ($errors) ) {
	$query = "SELECT user_id, username ";
	$query .= "FROM users ";
	$query .= "WHERE username =  '{$username}'";
	$query .= "AND hashed_password = '{$hashed_password}'";
	$query .= "LIMIT 1";
	$result_set = mysql_query($query);
	confirm_query($result_set);

	if(mysql_num_rows($result_set) == 1) {
		$found_user = mysql_fetch_array($result_set);
		$_SESSION['user_id'] = $found_user['user_id'];
		$_SESSION['username'] = $found_user['username'];
		redirect_to("staff.php");
		} else {
			$message = "Username/password combination incorrect.<br />
				Please make sure your caps lock key is off and try again.";
		}
		} else {
			if(count($errors) ==1){
				$message = "There was 1 error in the form.";
				} else {
					$message = "There were " . count($errors) . " errors in the form.";
				}
		}
		} else {
			if (isset($_GET['logout']) && $_GET['logout'] ==1) {
				$message = "You are now logged out.";
			}

			$username = "";
			$password = "";

 

I thank you in advance for reading this post, any help that can reslove this problem is very much appreciated. Many Thanks!

 

 

 

 

 

 

Link to comment
Share on other sites

You can't have any output, spaces HTML anything before a header() call.  You do here:

 

<?php require_once("includes/function.php"); ?>
these two blank lines are interpretted as HTML whitespace
and sent to the browser, because they aren't in php tags
<?php
session_start();

 

Should be:

<?php require_once("includes/function.php");

session_start();

 

Your other server must have had output buffering turned on in php.ini that buffered the output before the header.

Link to comment
Share on other sites

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.