Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 16th, 2004, 12:54 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 137
Thanks: 0
Thanked 0 Times in 0 Posts
Default Session Error / PHP.ini file


I am at present learning PHP and am currently learning all about sessions. The problem I have is with the config of PHP in use of sessions(as I do understand the concept). Basically, on running the simple program below, each time the browser is refreshed a new session is created. The example in the book states that the created session ID won't change as long as I have enabled COOKIES. I have checked my browser setting and all looks fine.

My question is, could there be a setting in the PHP.ini file that I need to enable / change? I have also disabled Norton Systems Works (Firewall etc..) and that doesn't seem to work!

I have changed session.auto_start = 1 in PHP.ini file, so I don't have to keep writing session_start() at the beginning of each script. And have since changed this back and used the code to see what happens, but I still have the same problem. Any ideas?

<html>
<head>
<title>Test script</title>
</head>
<body>
<?php

echo "<p>Your session ID is " .session_id() . "</p>";

?>
</body>
</html>


I have set up a localhost on my PC and have configured the use of PHP 4, Apache 2 and MySQL for the running of my code.

Gaz
__________________
Gaz
 
Old April 16th, 2004, 02:34 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Read my replies to the following topics:

http://p2p.wrox.com/topic.asp?TOPIC_ID=11791
http://p2p.wrox.com/topic.asp?TOPIC_ID=12148

Most importantly is session_start() must be called prior to using sessions.

Code:
<?php

    session_start();

?>
<html>
<head>
<title>Test script</title>
</head>
<body>
<?php

echo "<p>Your session ID is " .session_id() . "</p>";

?>
</body>
</html>
-Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old April 16th, 2004, 06:09 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 137
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for responding to my enquiry. I don't think I have explained my problem correctly. The above text code is used to create a session ID. The problem I faced is that on refreshing the browser the created session ID changes to a new session ID. Therefore, the following example would not work as it seems on every call of session_start() a new session ID is created.

 My question was, have I overlooked a configuration setting on the inital set-up of PHP to cause this problem? as I have uploaded the two below examples onto an actually live server and they work fine. I just cannot get the same result when run on a local server on my PC.

My understanding of sessions it that on first call of session_start() the ID created remains active and does'nt change when the browser in refreshed, unless the browser is closed and restarted. And that as long as COOKIES are enabled on the browser I shouldn't have the problem I currently face. Does that explain things better? as I am confused now?

The two problems below work fine on a live server, but not on my own local server:

<?php

session_start();

?>
<html>
<head><title>Listing 10.2 setting session variables</title></head>
<body>
<?php
$_SESSION[forename] = "Garry";
$_SESSION[surname] = "Butcher";

echo "Session variables have been created!";

?>
</body>
</html>


<?php

session_start();

?>
<html>
<head><title>Listing 10.3 session variables output</title></head>
<body>
<?php

echo "The data stored in your session variables are:<br>" .$_SESSION[forename] ." " .$_SESSION[surname];

?>
</body>
</html>



Gaz
 
Old April 16th, 2004, 06:57 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:Originally posted by socoolbrewster

My question was, have I overlooked a configuration setting on the inital set-up of PHP to cause this problem? as I have uploaded the two below examples onto an actually live server and they work fine. I just cannot get the same result when run on a local server on my PC.
Well that's what the other two threads are about, did you read my replies explicitly, nevermind the original topics.

On Windows the session data directory must be created and given write permissions explicitly or the session data is lost (hence an overlooked configuration setting), refer to topic posted at: http://p2p.wrox.com/topic.asp?TOPIC_ID=12148. This thread also refers to the php.ini configuration file, where all pre-set session configurations are stored.

What is the error_reporting directive of php.ini set to (to develop it should be E_ALL), if session_start() fails to create a session or send out a cookie it'll complain in the form of E_WARNING level errors. Are you seeing any error output?

Also look at these two and be sure that your errors aren't going to a log:
display_errors = On
log_errors = Off

Quote:
quote:Originally posted by socoolbrewster

My question is, could there be a setting in the PHP.ini file that I need to enable / change? I have also disabled Norton Systems Works (Firewall etc..) and that doesn't seem to work!
The firewall doesn't have anything to do with PHP sessions, since all sessions are kept on the server and anything output from PHP goes over port 80, unless you're trying to access the server on another port, then there might be a problem there.

Quote:
quote:Originally posted by socoolbrewster

My understanding of sessions it that on first call of session_start() the ID created remains active and does'nt change when the browser in refreshed, unless the browser is closed and restarted. And that as long as COOKIES are enabled on the browser I shouldn't have the problem I currently face. Does that explain things better? as I am confused now?
OK, session_start creates a session_id if one isn't currently active, it also imports all variables currently in that user's session data file into the current script (if any exist), these go into the $_SESSION superglobal. Then lastly, it outputs a cookie containing the session_id to the user's browser.

Read up on sessions:
http://www.php.net/session

hth,
Rich


::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old May 9th, 2006, 07:32 PM
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The characterset could be of influence.

Make sure you set the correct headers:

<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>....................etcetera

Setting the characterset to utf-8 triggered a new sessid each refresh because the cookie couldn't be set.

 
Old June 8th, 2006, 04:18 AM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Check your php.ini file for the line:

session.cookie_domain

Which will cause the behaviour you describe if it's incorrectly set.

If you are running your code on a development machine, there's no real reason to have it. So comment it out and restart Apache.








Similar Threads
Thread Thread Starter Forum Replies Last Post
session error - php.ini file bogins Beginning PHP 8 January 5th, 2008 04:59 PM
Ch 2; php.ini set up; /tmp file (where is it?) monster BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 February 21st, 2006 06:09 PM
PHP.ini and session.save_path variable ukbrit BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 March 9th, 2005 03:12 AM
php.ini allang Beginning PHP 1 November 20th, 2003 09:09 PM
php.ini include_path error problem paddy PHP How-To 1 July 21st, 2003 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.