Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Session Issues

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Session Issues

Postby mdm100 » Tue Sep 25, 2012 8:05 am

I keep getting the following error w/ session when running my authentication script. The script fails on any "header locaton call". Any help with issue would greatly appreciated. My code follows the error.

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/darkfiber/Login.php:23) in /var/www/html/darkfiber/Login.php on line 70


<?
session_start();

$username = $_POST['username'];
$password = $_POST['password'];
//connect to the database here
include('config.php');
$username = mysql_real_escape_string($username);
$query = "SELECT id, password, salt
FROM users
WHERE username = '$username';";
$result = mysql_query($query);

if(mysql_num_rows($result) < 1) // if no such user exists

{
header('Location: Login_form.php');
die();
}
$userData = mysql_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );
$userid = $userData['id'];

if($hash != $userData['password']) // if incorrect password
{
header('Location: Login_form.php');
die();
}
else
{
validateUser(); //sets the session data for this user
}
//redirect after successfull username and password comparison
header('Location: Index.php');
die();

// Functions
function validateUser()
{
session_start();
$_SESSION['valid'] = 1;
$_SESSION['userid'] = $userid;
}
?>
mdm100
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Sep 24, 2012 7:16 pm

Re: Session Issues

Postby egami » Wed Sep 26, 2012 2:35 pm

This usually occurs when you're sending out your headers before hand. ie.. you have like..

<?php
include ('headers.php'); // this file that contains <!DOCTYPE HTML.. etc..


is this file being "included" or is it being called from a form?
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Session Issues

Postby faust » Thu Sep 27, 2012 1:53 am

include('config.php');
If you have any output to the browser in this file, header() won't work.
faust
New php-forum User
New php-forum User
 
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Session Issues

Postby Harok » Mon Oct 01, 2012 10:49 am

Type a ob_start() before session_start() like this:

Code: Select all
ob_start();
session_start();


:)
Harok
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Oct 01, 2012 10:13 am

Re: Session Issues

Postby phpwebhosting » Tue Oct 02, 2012 1:00 pm

any output, including newlines at the top of the script before the php sections can cause this. delete all blank lines or, as mentioned above, use ob_start() to buffer all your output.
phpwebhosting
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Oct 02, 2012 12:54 pm

Re: Session Issues

Postby seandisanti » Tue Oct 02, 2012 1:12 pm

As they said, any output to the browser before the session_start() will throw error. that includes empty lines before <?php tag, OR if you have other php files included before that one that have trailing empty lines after a ?> closing tag. You can also bypass the error by enabling output_buffering in your php.ini (or manually each time as Harok suggested).
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 783
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.