...

View Full Version : No session in other pages



renzocj
11-11-2011, 09:48 PM
The session is created and stored in the page "login.php" using a login form but when I go to the second page, the session disappears. I have no idea why, really I have tried everything, I show the code, you may have a better view of the code than me (anybody):

This is the login.php



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();

include_once('overall.php');

if (isset($_POST['mail_log']) && isset($_POST['pass_log'])) {
//if the user has just tried to log in
$email=$_POST['mail_log'];
$password=$_POST['pass_log'];

$db_conn=new mysqli('***', '***', '***', '***');

if (mysqli_connect_errno()) {
echo '<h1>There is no answer from the database</h1>';
exit;
}

$query='select * from authorized_users '."where name='$email' "." and password=sha1('$password')";

$result=$db_conn->query($query);

if ($result->num_rows) {
//if they are in the database register the user id
$_SESSION['valid_user']=$email;
}
$db_conn->close();

}

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>...//rest of the page


In the heading of the login.php is an area created for welcome a member if he is registered in the database or create a form for a member who is not registered.

I did the test and this works very well, I can create a session with no problems with my email and password stored in the database, but when I go to the second page my session disappears.

This is the code for the second page:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();

include('overall.php');

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>


And in the heading of this second page, the welcome of the member should say "hello!" but shows "login" as no session exists...



<div id="login_area">
<?php

create_heading();

?>
</div><!--login_area-->


Thanks in advanced

mlseim
11-11-2011, 10:36 PM
You can't have anything before session_start();

This HAS to be the top of each script ...

<?php
session_start();


So change it up ...

<?php
session_start();
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";


like that instead.


.

renzocj
11-11-2011, 11:00 PM
above EVERYTHING ??? I will test this ... thanks in advanced !

renzocj
11-11-2011, 11:26 PM
Thank you very much, I really appreciate your help, sincerely I was at least like 5 hours trying to solve it, however I will never forget how to do it.... in my entire life....

tangoforce
11-11-2011, 11:41 PM
above EVERYTHING ??? I will test this ... thanks in advanced !

From PHP.net (http://uk3.php.net/session_start):


Note:

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.


For more information about this, see the headers already sent link in my signature.

renzocj
11-12-2011, 04:37 PM
Thank you tangoforce



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum