Stupid Errors with session_start()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

dimitris wrote:No Database?That means that require_once('connect.inc'); fails!
Seems like something's not right with that file - what's in it?

Mac
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

This file is included inorder to make a connection with the database!It is the same that was used to authenticate the user in the login form!

Code: Select all

<?php
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?php
function connect_db() {
    $db_server   =  'xxxxx';
    $db_user     =  'yyyyy';
    $db_password =  'zzzzzz';
    $db_database =  'db_name';
    $dbi = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
    mysql_select_db($db_database);
    return $dbi;
}
if (!isset($dbi))  $dbi = connect_db();
?>

</body>
</html>
?>
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post by llanitedave »

dimitris wrote:This file is included inorder to make a connection with the database!It is the same that was used to authenticate the user in the login form!

Code: Select all

<?php
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?php
function connect_db() {
    $db_server   =  'xxxxx';
    $db_user     =  'yyyyy';
    $db_password =  'zzzzzz';
    $db_database =  'db_name';
    $dbi = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
    mysql_select_db($db_database);
    return $dbi;
}
if (!isset($dbi))  $dbi = connect_db();
?>

</body>
</html>
?>
Looks like you called "<?php" twice, without a "?>" tag between them.
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

Sorry that was my mistake enabling phpbb code!
Thats the right code which i use:
The index page sabotages itself at the next stage!
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
</head>
<body bgcolor="#FFFFFF" text="#000000">

Code: Select all

<?php
function connect_db() {
    $db_server   =  'xxxxx';
    $db_user     =  'yyyyyy';
    $db_password =  'zzzzzzz';
    $db_database =  'my_db';
    $dbi = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
    mysql_select_db($db_database);
    return $dbi;
}
if (!isset($dbi))  $dbi = connect_db();

?>
</body>
</html>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It seems a bit strange that this has gone wrong - does changing:

Code: Select all

mysql_select_db($db_database);
to

Code: Select all

mysql_select_db($db_database) or die(mysql_error());
throw up a new error?

Mac
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

I changed it but the same error returns!
I have also to add that pages' title changed to Untitled Document
In my opinion something wrong is going on with the headers!

That's the first lines of my code

Code: Select all

<?php
				session_start();
				header("Cache-control: private"); 
				session_register('logged_in'); $logged_in=0;
				session_register('session_id'); $session_id=0;
				error_reporting(E_ALL);
				if(isset($HTTP_POST_VARS['submit'])){
				if((strlen($HTTP_POST_VARS['username'])>0) AND (strlen($HTTP_POST_VARS['password'])>0)){ ....		
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Ah, session_register() is deprecated and doesn't play nicely with $_SESSION, try:

Code: Select all

<?php
session_start();
header("Cache-control: private");

$_SESSION['logged_in'] = 0;
$_SESSION['session_id'] = 0;

error_reporting(E_ALL);
if (isset($_POST['submit'])) {
	if ((strlen($_POST['username']) > 0) && (strlen($_POST['password']) > 0)) {
		....
?>
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

Thanks a lot for the advise! I wasn't sure to use $_SESSION('x') whether session_register('x') but unfortunately the error continues!

Let me describe my page:
It is index.php with 4 php scripts which recall some records from some tables. There is a form which authenticates users. When a user submits the form he goes straight to the same page with the known results!

I will try to send the submition to another page (e.g. usercheck.php) and then with redirection back to index.php, with the suitable session variable.
I will try it and then inform you!

Of course the first error should also be resolved without this trick.
Post Reply