Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Session Help

General discussions related to php

Moderators: macek, egami, gesf

Session Help

Postby liquidchild_ » Wed Feb 26, 2003 12:51 am

I am having trouble with the following code and error:

Error:

Warning: Cannot add header information - headers already sent by (output started at C:\Intranet\php\login\session\session.class.php:40) in C:\Intranet\php\login\doLogin.class.php on line 72

Code:

/** SESSION CLASS **/

<?php

session_start();

class Session
{

function Session()
{
$this->_registerSession();
}

/** Register the session variables **/
function _registerSession()
{
/** Create the new session variables **/
session_register( 'username' );
session_register( 'privilege' );
session_register( 'firstname' );
session_register( 'logged' );

/** Register the default values **/
$_SESSION['username'] = NULL;
$_SESSION['privilege'] = NULL;
$_SESSION['firstname'] = NULL;
$_SESSION['logged'] = false;
}

/** Sets the session with $username, $privilege, $firstname and $logged **/
function setSession( $username, $privilege, $firstname, $logged )
{
$_SESSION['username'] = $username;
$_SESSION['privilege'] = $privilege;
$_SESSION['firstname'] = $firstname;
$_SESSION['logged'] = $logged;
}

}//End Session

?> //LINE 40 HERE

/** DOLOGIN CLASS **/
/** Performs the required login, sets the session variables and updates the users last login time **/
function _doLogin( $username, $privilege, $firstname )
{

/** Set the session variables username, privilege, firstname and set logged to now be true **/
//$this->_session->setSession( $username, $privilege, $firstname, 'true' );

/** Update the last Login **/
$this->_updateLastLogin( $username );

/** Redirect to the home page **/
header( "location: ../content/index.php" );
}

The only call to class session is at the beggining of class DOLOGIN when it is instantiated. I know that this is sending header information to the browser hence the error but can see no way round this, I have tried ob_flush etc - but does not seem to work! Any suggestion welcome.

Thanks
liquidchild_
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Feb 26, 2003 12:46 am

Re: Session Help

Postby WiZARD » Wed Feb 26, 2003 1:02 am

Strange question.....
what is it?
Code: Select all
/** Redirect to the home page **/
header( "location: ../content/index.php" );
}
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby Oleg Butuzov » Wed Feb 26, 2003 1:06 am

headers already send - it means that you have simething print before sending sesions headers....
Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby liquidchild_ » Wed Feb 26, 2003 1:14 am

I know that the headers are already being sent when i do the following:


function DoLogin()
{
ob_start();

$this->_session = new Session();

/** Setup the connection to the database **/
$this->_conn = new OdbcConn( 'kerrandsmith', 'login', 'login' );

$this->_checkLogin( $_POST['username'], $_POST['password'] );

/** Close the connection to the database **/
$this->_conn->closeConn( $this->_conn->getConn() );

ob_end_flush();
}

The headers are being sent when i create an new instance of Session, atleast that is what i think. However i thought that by doing ob_start() and flush this would sort the problem but it has not, and I am running out of ideas! Any suggestions.

Thanks
liquidchild_
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Feb 26, 2003 12:46 am

Postby xlordt » Wed Feb 26, 2003 8:46 am

You should just replace...

this..
Code: Select all
/** Redirect to the home page **/
header( "location: ../content/index.php" );
}


to this...
Code: Select all
echo '<meta http-equiv="refresh" content="2;url=../content/index.php(or url)">';
User avatar
xlordt
New php-forum User
New php-forum User
 
Posts: 34
Joined: Thu Dec 12, 2002 9:00 pm
Location: NJ

Postby Redcircle » Wed Feb 26, 2003 12:03 pm

1st. do not use session_register when using super global $_SESSION. it will act quirky or explode on you.

2nd. make sure that <?php is the very first thing in the file. make sure there is nothing output to the browser before <?php is called.

3rd header calls cannot be made after you see anything on the browser. like the 2nd thing I mentioned.

4th. If you do need to make a header call for a redirection make sure you include the FULL url. i.e http://www.yourserver.com/somepage.html older versions of Netscape, Opera, IE, Mozilla require the FULL url or will not work.
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA


Return to PHP General

Who is online

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

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

cron