fparvini
asked on
PHP Session doen'st get set
Hi ,
I need a simple visitor counter ,
I put this code in my index.php
<?php
if(isset($_SESSION['views' ])){
$_SESSION['views']=$_SESSI ON['views' ]+1;
echo "session is isset : Views=". $_SESSION['views']. "<BR><BR>";
} else {
$_SESSION['views']=1;
echo "session is Not set : Views=". $_SESSION['views']. "<BR><BR>";
}
?>
but every-time that I go back browsing that page, I get the message :
session is Not set : Views = 1
Thanks
I need a simple visitor counter ,
I put this code in my index.php
<?php
if(isset($_SESSION['views'
$_SESSION['views']=$_SESSI
echo "session is isset : Views=". $_SESSION['views']. "<BR><BR>";
} else {
$_SESSION['views']=1;
echo "session is Not set : Views=". $_SESSION['views']. "<BR><BR>";
}
?>
but every-time that I go back browsing that page, I get the message :
session is Not set : Views = 1
Thanks
ASKER
Thanks , I have it (and then removed) , but still doesn't work
I also added some debugging , as follows :
<?php
$a = session_id();
if(empty($a)) session_start();
echo "SID: ".SID."<br>session_id(): ".session_id()."<br>COOKIE : ".$_COOKIE["PHPSESSID"];
if(isset($_SESSION['views' ])){
$_SESSION['views']=$_SESSI ON['views' ]+1;
echo "isset Views=". $_SESSION['views']. "<BR><BR>";
} else {
$_SESSION['views']=1;
echo "Not set Views=". $_SESSION['views']. "<BR><BR>";
}
?>
but everytime I get different ids
SID: PHPSESSID=clfrk1b0heon60e2 3lsnrie613
session_id(): clfrk1b0heon60e23lsnrie613
COOKIE: Not set Views=1
SID: PHPSESSID=j2qai0obf52ch6c8 cfq4bh23s2
session_id(): j2qai0obf52ch6c8cfq4bh23s2
COOKIE: Not set Views=1
I also added some debugging , as follows :
<?php
$a = session_id();
if(empty($a)) session_start();
echo "SID: ".SID."<br>session_id(): ".session_id()."<br>COOKIE
if(isset($_SESSION['views'
$_SESSION['views']=$_SESSI
echo "isset Views=". $_SESSION['views']. "<BR><BR>";
} else {
$_SESSION['views']=1;
echo "Not set Views=". $_SESSION['views']. "<BR><BR>";
}
?>
but everytime I get different ids
SID: PHPSESSID=clfrk1b0heon60e2
session_id(): clfrk1b0heon60e23lsnrie613
COOKIE: Not set Views=1
SID: PHPSESSID=j2qai0obf52ch6c8
session_id(): j2qai0obf52ch6c8cfq4bh23s2
COOKIE: Not set Views=1
If you do a var_dump($_SESSION); what does it say? Put it preferably on the end fo your script.
ASKER
First time visiting page :
SID: PHPSESSID=ogusctqkfqqirtfe fjojko2vt7
session_id(): ogusctqkfqqirtfefjojko2vt7
COOKIE: Not set Views=1
array(1) { ["views"]=> int(1) }
second time :
SID: PHPSESSID=08r9t5c91ne0jpd9 mlkgmrv3j3
session_id(): 08r9t5c91ne0jpd9mlkgmrv3j3
COOKIE: Not set Views=1
array(1) { ["views"]=> int(1) }
SID: PHPSESSID=ogusctqkfqqirtfe
session_id(): ogusctqkfqqirtfefjojko2vt7
COOKIE: Not set Views=1
array(1) { ["views"]=> int(1) }
second time :
SID: PHPSESSID=08r9t5c91ne0jpd9
session_id(): 08r9t5c91ne0jpd9mlkgmrv3j3
COOKIE: Not set Views=1
array(1) { ["views"]=> int(1) }
You have to set seeeion start at the top
<?php
session_start();
mcnute's example is working, try refreshing the page few times.
A possible reason for your issue would be revealed when setting
error_reporting(E_ALL);
at the top also.
<?php
session_start();
mcnute's example is working, try refreshing the page few times.
A possible reason for your issue would be revealed when setting
error_reporting(E_ALL);
at the top also.
Ahahaaa, now I get where the flaw is: You cannot count visitors with storing the number of visitors in a session variable. You can start with this project. Simple and easy:
http://code.google.com/p/simphp/
http://code.google.com/p/simphp/
Let's run this test to see if your session handler is working correctly. Install this script and run it a few times. Click the appropriate places. If the "cheese" counter increments predictably your session works and the problem lies elsewhere in the code.
<?php // RAY_session_test.php
error_reporting(E_ALL);
// DEMONSTRATE HOW PHP SESSIONS WORK
// MAN PAGE HERE: http://php.net/manual/en/function.session-start.php
// START THE SESSION (DO THIS FIRST, UNCONDITIONALLY, IN EVERY PHP SCRIPT ON EVERY PAGE)
session_start();
// INITIALIZE THE SESSION ARRAY TO SET A DEFAULT VALUE
if (empty($_SESSION["cheese"])) $_SESSION["cheese"] = 1;
// SEE IF THE CORRECT SUBMIT BUTTON WAS CLICKED
if (isset($_POST['fred']))
{
// ADD ONE TO THE CHEESE
$_SESSION['cheese']++;
}
// RECOVER THE CURRENT VALUE FROM THE SESSION ARRAY
$cheese = $_SESSION['cheese'];
// END OF PROCESSING SCRIPT - CREATE THE FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<html>
<head>
<title>Session Test</title>
</head>
<body>
Currently, SESSION["cheese"] contains: $cheese<br/>
<form method="post">
<input type="submit" value="increment this cheese" name="fred" />
<input type="submit" value="leave my cheese alone" name="john" />
</form>
</body>
</html>
ENDFORM;
echo $form;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window