Jump to content

Session question.


leisnerr

Recommended Posts

Hi again, was trying to learn how to create and erase session's today. The code I came up with was this, my problem is that I thought it would be incrementing by 1 each time I refreshed the page, any tips?

 

<?php

 

session_start();

 

if(isset($_SESSION['views']))

$_SESSION['views']=$_SESSION['views']+1;

 

else

$_SESSION['views']=1;

echo "Views=". $_SESSION['views'];

 

?>

Link to comment
https://forums.phpfreaks.com/topic/43354-session-question/
Share on other sites

<?php

session_start();

echo "Views=". $_SESSION['views']; // echo this first to make sure there is a variable

if(isset($_SESSION['views']))
   $_SESSION['views']++;

else
   $_SESSION['views']=1;

echo "ViewsNow=". $_SESSION['views']; // see if it incremented

?>

 

The above should work, your old code should also work. Try this see what happens

Link to comment
https://forums.phpfreaks.com/topic/43354-session-question/#findComment-210542
Share on other sites

Okay I got it now. I'm a little confused on the isset command though if you could gimme a quick breifing on it. They didn't really explain it's purpose or what it primarily is defining, is it to check for what the variable $_SESSION['views'] actual value is? Or just to check if the function has been properly executed? Still a little confused on session's but what you said helped and I got mine working correctly now so thanks in advance. :-)

Link to comment
https://forums.phpfreaks.com/topic/43354-session-question/#findComment-210565
Share on other sites

If a variable isset (meaning it has been defined) than it returns true. If that variable has not been set or defined it returns false.

 

<?php
$cat = "fly";

if (isset($cat)) {
    print $cat . " has been set";
}

if (isset($someVar)) {
   print $someVar . " has also been set";
}else {
   print "someVar was not set!!!";
}

?>

 

Works for array indexes also, which is what you are doing with session, checking if an index of an array has been set/defined.

Link to comment
https://forums.phpfreaks.com/topic/43354-session-question/#findComment-210569
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.