Solved

PHP Session doen'st get set

Posted on 2013-02-01
8
245 Views
Last Modified: 2013-10-12
Hi ,
I need a simple visitor counter ,
I put this code in my index.php

<?php

if(isset($_SESSION['views'])){
        $_SESSION['views']=$_SESSION['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
0
Comment
Question by:fparvini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 11

Expert Comment

by:mcnute
ID: 38844528
add session_start(); at the top of your script.

<?php
session_start();
if(isset($_SESSION['views'])){
        $_SESSION['views']=$_SESSION['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>";
}
?>

Open in new window

0
 

Author Comment

by:fparvini
ID: 38844582
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']=$_SESSION['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=clfrk1b0heon60e23lsnrie613
session_id(): clfrk1b0heon60e23lsnrie613
COOKIE: Not set Views=1


SID: PHPSESSID=j2qai0obf52ch6c8cfq4bh23s2
session_id(): j2qai0obf52ch6c8cfq4bh23s2
COOKIE: Not set Views=1
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38844592
If you do a var_dump($_SESSION); what does it say? Put it preferably on the end fo your script.
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:fparvini
ID: 38844635
First time visiting page :

SID: PHPSESSID=ogusctqkfqqirtfefjojko2vt7
session_id(): ogusctqkfqqirtfefjojko2vt7
COOKIE: Not set Views=1

array(1) { ["views"]=> int(1) }


second time :
SID: PHPSESSID=08r9t5c91ne0jpd9mlkgmrv3j3
session_id(): 08r9t5c91ne0jpd9mlkgmrv3j3
COOKIE: Not set Views=1

array(1) { ["views"]=> int(1) }
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38844649
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.
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38844656
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/
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38844664
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;

Open in new window

0
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 200 total points
ID: 38846495
Report all errors using:
error_reporting(-1);

Open in new window


Create test.php and use the following code:
// File: test.php
error_reporting(-1);
session_write_close();
session_start();
$_SESSION['test'] = 'Session data...';
echo $_SESSION['test'];

Open in new window


The above code should outputs: "Session data..."

If the above code doesn't work, then you probably have configuration problem, perhaps the timeout is set to expire instantly?
0

Featured Post

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question