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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP sessions - not enabled? 1

Status
Not open for further replies.

atsea

Technical User
Feb 27, 2005
51
JP
I just started working with PHP sessions and have ran into a problem.

I was given a simple code that would allow me to see if I had sessions working or not. When running the code I could see that new session ID's were being created in the C:\Temp folder (as specified in the php.ini file) but the session seem to die after that. In other words the session is not maintained.

Could this be a problem with my php.ini configuration? Apache configuration?

Currently I'm using PHP 5.0.4 with Apache 2.0.53

And, I'm using this code to test session availability:
Code:
<?php
session_start();
if(!isset($_SESSION['test'])) {
   $_SESSION['test'] = 0;
} else {
    echo $_SESSION['test'];
    $_SESSION['test']++;
}
?>
As mentiond when running this code session ID's are created, however nothing seems to print (or increment)

Also, here is the session segment of my php.ini file:
(is there anything wrong with it?)
Code:
[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler.  In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
;
; As of PHP 4.0.1, you can define the path as:
;
     session.save_path = "C:\Temp"
;
; where N is an integer.  Instead of storing all the session files in
; /path, what this will do is use subdirectories N-levels deep, and
; store the session data in those directories.  This is useful if you
; or your OS have problems with lots of files in one directory, and is
; a more efficient layout for servers that handle lots of sessions.
;
; NOTE 1: PHP will not create this directory structure automatically.
;         You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
;         use subdirectories for session storage
;
; The file storage module creates files using mode 600 by default.
; You can change that by using
;
;     session.save_path = "N;MODE;/path"
;
; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
;session.save_path = "/tmp"

; Whether to use cookies.
session.use_cookies = 1

; This option enables administrators to make their users invulnerable to
; attacks which involve passing session ids in URLs; defaults to 0.
; session.use_only_cookies = 1

; Name of the session (used as cookie name).
session.name = PHPSESSID

; Initialize session on request startup.
session.auto_start = 0

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 0

; The path for which the cookie is valid.
session.cookie_path = \

; The domain for which the cookie is valid.
session.cookie_domain =

; Handler used to serialize data.  php is the standard serializer of PHP.
session.serialize_handler = php

; Define the probability that the 'garbage collection' process is started
; on every session initialization.
; The probability is calculated by using gc_probability/gc_divisor,
; e.g. 1/100 means there is a 1% chance that the GC process starts
; on each request.

session.gc_probability = 1
session.gc_divisor     = 1000

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

; NOTE: If you are using the subdirectory option for storing session files
;       (see session.save_path above), then garbage collection does *not*
;       happen automatically.  You will need to do your own garbage
;       collection through a shell script, cron entry, or some other method.
;       For example, the following script would is the equivalent of
;       setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
;          cd /path/to/sessions; find -cmin +24 | xargs rm

; PHP 4.2 and less have an undocumented feature/bug that allows you to
; to initialize a session variable in the global scope, albeit register_globals
; is disabled.  PHP 4.3 and later will warn you, if this feature is used.
; You can disable the feature and the warning separately. At this time,
; the warning is only displayed, if bug_compat_42 is enabled.

session.bug_compat_42 = 0
session.bug_compat_warn = 1

; Check HTTP Referer to invalidate externally stored URLs containing ids.
; HTTP_REFERER has to contain this substring for the session to be
; considered as valid.
session.referer_check =

; How many bytes to read from the file.
session.entropy_length = 0

; Specified here to create the session id.
session.entropy_file =

;session.entropy_length = 16

;session.entropy_file = /dev/urandom

; Set to {nocache,private,public,} to determine HTTP caching aspects
; or leave this empty to avoid sending anti-caching headers.
session.cache_limiter = nocache

; Document expires after n minutes.
session.cache_expire = 180

; trans sid support is disabled by default.
; Use of trans sid may risk your users security.
; Use this option with caution.
; - User may send URL contains active session ID
;   to other person via. email/irc/etc.
; - URL that contains active session ID may be stored
;   in publically accessible computer.
; - User may access your site with the same session ID
;   always using URL stored in browser's history or bookmarks.
session.use_trans_sid = 0

; Select a hash function
; 0: MD5   (128 bits)
; 1: SHA-1 (160 bits)
session.hash_function = 0

; Define how many bits are stored in each character when converting
; the binary hash data to something readable.
;
; 4 bits: 0-9, a-f
; 5 bits: 0-9, a-v
; 6 bits: 0-9, a-z, A-Z, "-", ","
session.hash_bits_per_character = 5

; The URL rewriter will look for URLs in a defined set of HTML tags.
; form/fieldset are special; if you include them here, the rewriter will
; add a hidden <input> field with the info which is otherwise appended
; to URLs.  If you want XHTML conformity, remove the form entry.
; Note that all valid entries require a "=", even if no value follows.
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"

any help would be greatly appreciated.

thanks

atsea
 
is there a value in the session file?
Code:
session.save_path = "C:\Temp"
try
Code:
<?php
session_start();
if(!isset($_SESSION['test'])) {
   $_SESSION['test'] = 0;
} else {    
    $_SESSION['test']++;
}
$test = $_SESSION['test'];
echo 'session was '. $test;
?>
sometimes echo ing a session gives me trouble?

A problem with no solution is a problem viewed from the wrong angle
 
The first time you run your code, nothing will print. The next time and anytime after when you run your code from the same browser session, you should get something.

If you put the line
Code:
echo '<pre>'; print_r($_SESSION);echo '</pre>';
immediately after the session_start() line, what prints?

Ken
 
litton1 - The value in the session file (for both your test script and mine) is:

test|i:0;

kenrbnsn - If I put that line you suggested immediately after the session_start() line I get this:

Array
(
)

Each time I "refresh" my test code a new session variable is created...shouldn't it be using the same one?

atsea
 
atsea said:
I was given a simple code that would allow me to see if I had sessions working or not.

Why didn't try with phpinfo()?

there is a complete section about sessions, example (from my phpinfo):

Code:
session
Session Support 	enabled
Registered save handlers 	files user sqlite
Registered serializer handlers 	php php_binary

Directive	Local Value	Master Value
session.auto_start	Off	Off
session.bug_compat_42	On	On
session.bug_compat_warn	On	On
session.cache_expire	180	180
session.cache_limiter	nocache	nocache
session.cookie_domain	no value	no value
session.cookie_lifetime	0	0
session.cookie_path	/	/
session.cookie_secure	Off	Off
session.entropy_file	no value	no value
session.entropy_length	0	0
session.gc_divisor	100	100
session.gc_maxlifetime	1440	1440
session.gc_probability	1	1
session.hash_bits_per_character	4	4
session.hash_function	0	0
session.name	PHPSESSID	PHPSESSID
session.referer_check	no value	no value
session.save_handler	files	files
session.save_path	no value	no value
session.serialize_handler	php	php
session.use_cookies	On	On
session.use_only_cookies	Off	Off
session.use_trans_sid	0	0

Cheers.
 
if u get 0 then it has been set so the fault is elsewhere, did u try the code i posted?

A problem with no solution is a problem viewed from the wrong angle
 
Sorry I didn’t read your post correctly, there is no reason php wise why this dies unless a page is visited with no session_start(); at the top, does this help?

A problem with no solution is a problem viewed from the wrong angle
 
Chacalinc - That was one of the first things I did...To me the session section appeared fine (although I'm no expert on the php.ini file so I could be wrong). It was not until I started programming with sessions that I noticed somthing wrong. I have used many different test scripts but for some reason the session isn't maintained.

litton1 - "there is no reason php wise...." - If its not an issue with my php (as I'm sure I have session_start(); at the top of each script) what else could it be? Could it be somthing with my Apache httpd.confg file?

Thanks for looking into this folks...keep the suggestions comming, this is driving me insane, I'm sure there's an easy explination that I'm overlooking.

atsea
 
Code:
Could it be somthing with my Apache httpd.confg file?
no I don’t think so as php is working, I am assuming that you are doing this test in a short script, if yes put your code on hear

A problem with no solution is a problem viewed from the wrong angle
 
litton1 - I've used many different little scripts to test this problem but they all essentially do the same thing...Here are the two that I'm using now:

Code:
<?php
session_start();

if(!isset($_SESSION['test'])) {
   $_SESSION['test'] = 0;
} else {
    echo $_SESSION['test'];
    $_SESSION['test']++;
}
?>

and...

Code:
<?php
session_start();
session_register("count");

if (!isset($_SESSION)) 
{
	$_SESSION["count"] = 0;
	echo "<p>Counter initialized</p>\n";
} 
else { $_SESSION["count"]++; }

echo "<p>The counter is now <b>$_SESSION[count]</b></p>".
	"<p>please reload this page to increment</p>";
?>

The same thing happens with both...
The session appears to be created in the C:\Temp file (as specified in the php.ini file) when the script is run for the first time.

everytime the script is refreshed an entirly new session file is created and the original session is never incremented.


Lrnmore - That possibility had crossed my mind, unfortunatly it was not the case.

Thanks,

atsea
 
Try a forward slash,

Code:
; The path for which the cookie is valid.
session.cookie_path = \

to

Code:
; The path for which the cookie is valid.
session.cookie_path = /

 
YOU ARE A GENIOUS!!!!!

I can't believe that caused me almost two weeks of grief...

You have just spared my computers life.

Thank you

atsea
 
Anytime, glad it was a help.

Thanks for the
star.gif


Happy we a avoided the computer smashing.
 
Alright, before I destroy my computer I figured I'd throw this question here, since you guys were so much help with my first php.ini issue.

With sessions now working I decided to test the mail() function to make sure there was nothing wrong with that. Of course, it doesn't work.

here is the Mail Function Section of my php.ini file.
Code:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

I have configured this file before without any problems. The only difference now is that I'm working on a japanese computer (the charcter set is a little different, which may explaine some of the little differences in the php.ini i.e. / and \.

There is probably a simple solution to this as well, but that last issue has left me blind with rage.

Thanks

atsea
 
You've told PHP to use your windows box to send email. Do you have a Mail Server running on the box? If not, there's your problem.

Ken
 
atsea

At TT it is customary to start a new thread for a new question. Tagging a new question to an old thread will also give your question much less exposure because only people already watching the thread are likely to encounter your new question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top