Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session_Start issue

Status
Not open for further replies.

AlanArons

Programmer
Aug 1, 2002
91
US
I have an issue with sessions and created the following code to demonstrate. In the following example, I would expect the Old and New Count to increment each time the screen was refreshed, but they don't. It seems that the $_Session variables are reset each time.

Any Thoughts?

Alan Arons [ponder]

Code:
<?php
session_start();
  if ( ! isset( $_SESSION))
	{
  echo "Session was not set";
  exit ;
  }
  else
  {
     $oldcount=$_SESSION['count'];
     echo "Old count is ".$oldcount." <br />";
      $_SESSION['count']++ ;
   }
  echo "New count is ",$_SESSION['count'],". <br />";
  echo "The session id is: ",session_id();
?>
 
Here is what I get running your exact code

First run:

Notice: Undefined index: count in /var/ on line 10
Old count is

Notice: Undefined index: count in /var/ on line 12
New count is 1.
The session id is: 9itjjhc77qj8lvlreigshv2gr6

second run:

Old count is 1
New count is 2.
The session id is: 9itjjhc77qj8lvlreigshv2gr6

third run:

Old count is 2
New count is 3.
The session id is: 9itjjhc77qj8lvlreigshv2gr6

So my guess is a configuration issue. Can you share phpinfo(), or at least the session section of it?

-----------------------------------------
I cannot be bought. Find leasing information at
 
Your output is what I would expect. Any ideas what would stop mine from working?

I dont get the warnings, just the following over and over

Old count is
New count is 1.
The session id is: cf11a8984718f5567f7efeced22645a7

Alan Arons [ponder]
 
Sorry, Pressed enter before the following

PHP Version 5.2.4

System Linux boscgi1802.eigbox.net 2.6.30.6 #1 SMP Mon Sep 14 16:36:43 EDT 2009 i686
Build Date Sep 10 2007 12:20:07
Configure Command './configure' '--prefix=/usr/local/lib/php-5.2.4' '--without-apache' '--with-mysql=/usr' '--with-mysqli' '--with-gd' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--with-tiff-dir=/usr' '--disable-debug' '--enable-discard-path' '--enable-inline-optimization' '--enable-mbstring' '--enable-mbstr-enc-trans' '--enable-memory-limit' '--with-regex=system' '--with-zlib' '--enable-ftp' '--enable-sockets' '--enable-magic-quotes' '--with-gettext' '--enable-wddx' '--with-gdbm' '--with-db4' '--with-freetype-dir=/usr' '--with-curl=/usr' '--with-mhash' '--with-xsl' '--enable-calendar' '--with-mcve' '--with-dom' '--with-iconv' '--with-xmlrpc' '--with-mcrypt' '--with-bzip2' '--with-ming=/usr' '--with-pspell' '--with-openssl' '--with-ttf' '--with-freetype-dir=/usr' '--enable-gd-native-ttf' '--with-imap' '--with-kerberos' '--with-imap-ssl' '--enable-bcmath' '--enable-dbase' '--enable-exif'
Server API CGI
Virtual Directory Support disabled
Configuration File (php.ini) Path /usr/local/lib/php-5.2.4/lib
Loaded Configuration File /usr/local/lib/php-5.2.4/lib/php.ini
PHP API 20041225
PHP Extension 20060613
Zend Extension 220060519
Debug Build no
Thread Safety disabled
Zend Memory Manager enabled
IPv6 Support enabled
Registered PHP Streams php, file, data, http, ftp, compress.zlib, https, ftps
Registered Stream Socket Transports tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
Registered Stream Filters string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, convert.iconv.*, zlib.*

Alan Arons
 
Hmm, is there any chance that your browser is reloading a cached version of the page? My guess is if the session id doesn't change and the count doesn't change, the php might not be getting executed again. What happens when you run this, then click the submit button?

Code:
[axtell@Axtell4 html]$ cat session.php
<?php
session_start();
  if ( ! isset( $_SESSION))
    {
  echo "Session was not set";
  exit ;
  }
  else
  {
     $oldcount=$_SESSION['count'];
     echo "Old count is ".$oldcount." <br />";
      $_SESSION['count']++ ;
   }
  echo "New count is ",$_SESSION['count'],". <br />";
  echo "The session id is: ",session_id(),"<br/>" ;
//phpinfo();

$counter = $_GET['counter'];
if (! $counter >0){
$counter = 0;
}
echo $counter;
$counter = $counter + 1;
echo "<form method=\"get\" action=\"session.php\"/>";
echo "<input type=\"hidden\" name=\"counter\" value=\"" . $counter . "\"/>";
echo "<input type=\"submit\"/>";
echo "</form>";
?>

-----------------------------------------
I cannot be bought. Find leasing information at
 
The first time I load the page I get
Old count is
New count is 1.
The session id is: cf11a8984718f5567f7efeced22645a7
0
[Submit Button}

When I click Submit, I get a Page Not Found Error

 
you need to change the form action to go to whatever your page is called. mine was session.php

-----------------------------------------
I cannot be bought. Find leasing information at
 
My dumb. Sorry.

so now that I have that fixed, the session counters don't change. $counter that was part of your code changes as does the parameter being passed

Alan

BTW, Jaxtell, I really appreciate your help with this.
 
please try this code instead

Code:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
session_start();
$oldcount = $_SESSION['count'];
echo "Old count is $_SESSION[count] <br />";
$_SESSION['count']++ ;
echo "New count is " . $_SESSION['count'] . ". <br />";
echo "The session id is: " . session_id();
session_write_close();
?>
 
First, I got several errors relating to having the session_start on line 3, so I moved it to the first line. That helped.

I got the following errors and output:
Code:
Notice: Undefined index: count in ... testlogin0.php on line 5
Notice: Undefined index: count in ... testlogin0.php on line 6
Old count is

Notice: Undefined index: count in ... testlogin0.php on line 7
New count is 1.
The session id is: 0e604d82814c0b732bac83c35a2a891b

Warning: session_write_close() [function.session-write-close]: open(/var/php_sessions/sess_... O_RDWR) failed: No such file or directory (2) in ...testlogin0.php on line 10

Warning: session_write_close() [function.session-write-close]: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_sessions) in .../testlogin0.php on line 10
Your help in deciphering these messages is greatly appreciated.

Alan


 
and that, i think, explains the problem!

the undefined index notices are just the initial state. they will not harm but can be avoided by this code
Code:
$oldcount = (empty($_SESSION['count'])) ? 0 : $_SESSION['count'];

the other errors indicate that the value for session.save_path in your php.ini file is pointing to a directory that does not exist. edit the php.ini and change the setting to a location that exists and for which the php process has read/write permissions. then save the file and restart the web server. all should then be well.
 
jpadie, I edited the php.ini file per your suggestion. Didn't really do anything to start, just added a blank space to the end of a comment line and saved.

Apparently, the php.ini file was in some way corrupt or not interacting properly. And apparently, re-saving it cleared whatever problem existed. Anyway, it all now works as it should. sigh.

Thanks everyone for your help.

I love computers.

Alan Arons [ponder]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top