$_SESSION variable not working?

software development

#1

Hi all… I’m trying to get PHP Sessions working for the first time on my site… and can’t seem to get the $_SESSION variable to “stick.” I’m using something simple like below. With just this code on the page, I would expec the “views” to count up each time I call the page… but it doesn’t. And when I include a phpinfo() call, I can’t see that the $_SESSION variable is in the list at all. Am I missing something? Is this even allowed on the Dreamhost servers? Thanks!

<?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; echo "views = ". $_SESSION['views']; ?>

EDIT: I should add that I’m running in Firefox with cookies off. With cookies turned on, it seems to work… but the thing is, I want to capture session data without having to place a cookie (or for when the user doesn’t accept them). Thanks!


#2

[quote=“triggerx, post:1, topic:56200”]EDIT: I should add that I’m running in Firefox with cookies off. With cookies turned on, it seems to work… but the thing is, I want to capture session data without having to place a cookie (or for when the user doesn’t accept them). Thanks!
[/quote]

Well that pretty much explains it! Look, to maintain a session you have to have the browser provide a token identifier. If you insist on not using cookies to do so this means you have to rely on the URLs using a query string parameter instead.

See Passing the Session ID - as you will notice in the example you have to use PHP code to generate/modify URLs in the HTML output, and that has some caveats.