Connecting Tech Pros Worldwide Forums | Help | Site Map

session_start failure

Florence HENRY
Guest
 
Posts: n/a
#1: Jul 17 '05
Hello,

I've searched about my problem on google before posting, but I didn't find
anything relevant...

I have a problem with session_start. Here is my code :

<html>
<head>
<?php session_start();
$id = session_id();
$_SESSION['color'] = 'green'; ?>
<title>PHP test</title>
</head>
<body>
this is my ID : <?php echo $id; ?>
<br />
these are my data : <?php print_r($_SESSION); ?>
</body>
</html>

the value printed by "echo $id" or "echo session_id()" is empty!
As I use post or get to transmit the session id, my scripts don't work.

The reason may be in the log. Here are the messages I get when I try to
execute the code above :

[Tue Mar 30 18:12:02 2004] [error] PHP Warning: session_start(): The
session id contains invalid characters, valid characters are only a-z, A-Z
and 0-9 in /home/www/session.php on line 3
[Tue Mar 30 18:12:02 2004] [error] PHP Warning: Unknown(): The session id
contains invalid characters, valid characters are only a-z, A-Z and 0-9 in
Unknown on line 0
[Tue Mar 30 18:12:02 2004] [error] PHP Warning: Unknown(): Failed to write
session data (files). Please verify that the current setting of
session.save_path is correct (/tmp) in Unknown on line 0

How can the session id contain invalid characters, as it is PHP who decides
it!

I read many times the doc, I do not understand where I'm wrong...

Thanks for any tip.

--
Florence HENRY
florence point henry arobasse obspm point fr

kingofkolt
Guest
 
Posts: n/a
#2: Jul 17 '05

re: session_start failure


Florence --

session_start() sends a cookie to the browser. A cookie is sent via the
headers, which have to be sent to the browser BEFORE anything else (such as
HTML) is sent. Attempting to set a cookie after you have already output HTML
(the HTML you had output was "<html>" and "<head>") will result in an error.
You have to move the session_start() command to the very beginnning of your
PHP page. So your code should be changed to look like this:

<?php session_start();
$id = session_id();
$_SESSION['color'] = 'green'; ?>
<html>
<head>
<title>PHP test</title>
</head>
<body>
this is my ID : <?php echo $id; ?>
<br />
these are my data : <?php print_r($_SESSION); ?>
</body>
</html>

"Florence HENRY" <raslebol@duspam.fr> wrote in message
news:c4cdas$p4r$1@carbone.net.espci.fr...[color=blue]
> Hello,
>
> I've searched about my problem on google before posting, but I didn't find
> anything relevant...
>
> I have a problem with session_start. Here is my code :
>
> <html>
> <head>
> <?php session_start();
> $id = session_id();
> $_SESSION['color'] = 'green'; ?>
> <title>PHP test</title>
> </head>
> <body>
> this is my ID : <?php echo $id; ?>
> <br />
> these are my data : <?php print_r($_SESSION); ?>
> </body>
> </html>
>
> the value printed by "echo $id" or "echo session_id()" is empty!
> As I use post or get to transmit the session id, my scripts don't work.
>
> The reason may be in the log. Here are the messages I get when I try to
> execute the code above :
>
> [Tue Mar 30 18:12:02 2004] [error] PHP Warning: session_start(): The
> session id contains invalid characters, valid characters are only a-z, A-Z
> and 0-9 in /home/www/session.php on line 3
> [Tue Mar 30 18:12:02 2004] [error] PHP Warning: Unknown(): The session id
> contains invalid characters, valid characters are only a-z, A-Z and 0-9 in
> Unknown on line 0
> [Tue Mar 30 18:12:02 2004] [error] PHP Warning: Unknown(): Failed to[/color]
write[color=blue]
> session data (files). Please verify that the current setting of
> session.save_path is correct (/tmp) in Unknown on line 0
>
> How can the session id contain invalid characters, as it is PHP who[/color]
decides[color=blue]
> it!
>
> I read many times the doc, I do not understand where I'm wrong...
>
> Thanks for any tip.
>
> --
> Florence HENRY
> florence point henry arobasse obspm point fr[/color]


Andreas Paasch
Guest
 
Posts: n/a
#3: Jul 17 '05

re: session_start failure


According to Vulcan logic - or learning thereof - kingofkolt wrote:
[color=blue]
> Florence --
>
> session_start() sends a cookie to the browser. A cookie is sent via the
> headers, which have to be sent to the browser BEFORE anything else (such
> as HTML) is sent. Attempting to set a cookie after you have already output
> HTML (the HTML you had output was "<html>" and "<head>") will result in an
> error. You have to move the session_start() command to the very beginnning
> of your PHP page. So your code should be changed to look like this:
>
> <?php session_start();
> $id = session_id();
> $_SESSION['color'] = 'green'; ?>
> <html>
> <head>
> <title>PHP test</title>
> </head>
> <body>
> this is my ID : <?php echo $id; ?>
> <br />
> these are my data : <?php print_r($_SESSION); ?>
> </body>
> </html>
>
> "Florence HENRY" <raslebol@duspam.fr> wrote in message
> news:c4cdas$p4r$1@carbone.net.espci.fr...[color=green]
>> Hello,
>>
>> I've searched about my problem on google before posting, but I didn't
>> find anything relevant...
>>
>> I have a problem with session_start. Here is my code :
>>
>> <html>
>> <head>
>> <?php session_start();
>> $id = session_id();
>> $_SESSION['color'] = 'green'; ?>
>> <title>PHP test</title>
>> </head>
>> <body>
>> this is my ID : <?php echo $id; ?>
>> <br />
>> these are my data : <?php print_r($_SESSION); ?>
>> </body>
>> </html>
>>
>> the value printed by "echo $id" or "echo session_id()" is empty!
>> As I use post or get to transmit the session id, my scripts don't work.
>>
>> The reason may be in the log. Here are the messages I get when I try to
>> execute the code above :
>>
>> [Tue Mar 30 18:12:02 2004] [error] PHP Warning: session_start(): The
>> session id contains invalid characters, valid characters are only a-z,
>> A-Z and 0-9 in /home/www/session.php on line 3
>> [Tue Mar 30 18:12:02 2004] [error] PHP Warning: Unknown(): The session
>> [id
>> contains invalid characters, valid characters are only a-z, A-Z and 0-9
>> in Unknown on line 0
>> [Tue Mar 30 18:12:02 2004] [error] PHP Warning: Unknown(): Failed to[/color]
> write[color=green]
>> session data (files). Please verify that the current setting of
>> session.save_path is correct (/tmp) in Unknown on line 0
>>
>> How can the session id contain invalid characters, as it is PHP who[/color]
> decides[color=green]
>> it!
>>
>> I read many times the doc, I do not understand where I'm wrong...
>>
>> Thanks for any tip.
>>
>> --
>> Florence HENRY
>> florence point henry arobasse obspm point fr[/color][/color]

Damn, I should have seen that. I was looking at it, but didn't see the
obvious.

/Andreas
--
Peace and long life ...
Registeret Linux user #292411
Florence HENRY
Guest
 
Posts: n/a
#4: Jul 17 '05

re: session_start failure


kingofkolt a écrit :
[color=blue]
> session_start() sends a cookie to the browser. A cookie is sent via the
> headers, which have to be sent to the browser BEFORE anything else (such
> as HTML) is sent. Attempting to set a cookie after you have already output
> HTML (the HTML you had output was "<html>" and "<head>") will result in an
> error. You have to move the session_start() command to the very beginnning
> of your PHP page. So your code should be changed to look like this:[/color]

Thanks for your explanation. It works nice now!

Obviously, I did not understand what the manual said... I did not undersand
that cookies was the default method used, then I did not take care of the
sentence saying "If you are using cookie-based sessions, you must call
session_start() before anything is outputted to the browser."


--
Florence HENRY
florence point henry arobasse obspm point fr
Closed Thread