I think this code should echo "first" in first usage and after refresh it should echo timestamp,but it will show first every time,where is my problem?
I set cookie permition on always.
if (isset($_SESSION['TestSession']))
{
echo ($_SESSION['TestSession']);
}
else
{
echo "first";
$_SESSION['TestSession'] = time();
}
Have you placed
session_start();
at the top of your page?
To start a session, first session_start() should be called. Otherwise $_SESSION won't be set, and you will initialize it every time.
Related
How to access the incremented value of session on the same page where we have defined the initial value of session, I am using this method for incrementation of value but on another page
<?php
$_SESSION['indexValue'] = 1;
?>
This is my test.php page where i have decrelaed the inital value of session.
<?php
$Incrementvalue = $_SESSION['indexValue'];
$counter = (int)$Incrementvalue;
if ($counter=$counter) {
$counter++;
$_SESSION['indexValue'] = $counter;
}
echo "$_SESSION['indexValue']";
?>
This is my getdata.php page where I have implmented the increment value function. Now i have to pass this increment value of Session again on test.php page . How can I perform this?
your code seems not to be thought off
first of all, you need to call session_start() at beginning
then you can access everyewhere your $_SESSION variable (if you call session_start() before)
if ($counter=$counter) {
$counter++;
$_SESSION['indexValue'] = $counter;
}
this seems useless
$_SESSION['indexValue']++;
is enough
First of all there are few errors in code like
if ($counter=$counter) should be if ($counter == $counter)
And
echo "$_SESSION['indexValue']" should be echo $_SESSION['indexValue'].
Now answer to your question is that session is accessible anywhere throughout the project/application. So you need not to pass it , just access it as $_SESSION['indexValue'].
I have a problem with sessions in wordpress. First I've activated the use of sessions in my functions.php and no I get the right variable but just after I reload my target site after setting the variable. When I load another page first and my target page seacondly my variable has a wrong value without any context to my first value. When I have the right value on my target site and I reload this site the value is also wrong. This is my code:
//Activate sessions() in functions.php
add_action('init', function(){
if(!session_id())
{
session_start();
}
}, 1);
//Calculate the variable
$gesamtumsatz_kcal = round (($value1 + $value2 + $value3), 0);
//Then set the session variable
$_SESSION['gesamtumsatz_kcal'] = $gesamtumsatz_kcal;
//Get my session variable on another page somewhere on my site
if(isset($_SESSION['gesamtumsatz_kcal'])) {
$gesamtumsatz_kcal = $_SESSION['gesamtumsatz_kcal'];
} else {
$gesamtumsatz_kcal = '';
}
//Echo my variable
echo $gesamtumsatz_kcal;
Do you have any idea whats wrong? I'm absolutely at the end with this....
Thanks a lot!
I have to add this
When I define a constant variable without calculate something like this:
//Define the variable
$_SESSION['test'] = 1056;
//Get the variable
$test = $_SESSION['test'];
//Echo the variable
echo $test;
I always get the right value: 1056 back... What is this? Can't uderstand
wordpres destroys session when it is not opened. so use
if(!session_id()) session_start();
in the start of wp-config.php or better in the init of your code/plugin
I have a session variable that is updated when a link is clicked which adds GET variables to the current URL:
<?php
$_SESSION['test'] = 0;
if (isset($_GET['do'] && $_GET['do'] == 'this')) {
$_SESSION['test'] = $_SESSION['test'] + 1;
}
echo $_SESSION['test'];
?>
Click me
When the link is clicked, the new session value that is echoed is still 0.
Why? How do I get this to echo the updated value? (1)
EDIT: Yes, session_start() is included at the top of the page.
change it to be like this:
<?php
session_start();
if(!array_key_exists('test', $_SESSION)){
$_SESSION['test'] = 0;
}
if(array_key_exists('do', $_GET) && $_GET['do'] === 'this') {
$_SESSION['test']++;
}
echo $_SESSION['test'];
?>
Click me
I like array_key_exists() more than isset because the key can exist being null and still pass the test:
From the php docs: "isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does."
You also were using isset incorrectly to test the contents of $_GET['do']. To top things off, you didn't start the session on each page load.
session_start() will initialize the $_SESSION variable and fill it with the appropriate contents. Since this was not in your code, the $_SESSION variable never had your 'do' key.
The second-to-last problem you had was the very first line, you were always setting the 'test' key to 0 causing the outcome to always be 1 after clicking Click me
and finally, you didn't close the php tag prior to outputting raw HTML which should have given you an error
Maybe you have not paste the whole code but here is how it should have been done.
<?php
session_start();
$_SESSION['test'] = 0;
if (isset($_GET['do']) && $_GET['do'] == 'this') {
$_SESSION['test'] += 1;
}
echo $_SESSION['test'];
?>
Click me
You need to start with session_start() Google to learn more about it.
You have put everything in the isset() Google isset() to learn more about it.
you need to close php tags before you can insert raw HTML. Inside HTML you can execute php with <?php ?> tags.
Hope this helps.
As mentioned before, you have everything wrapped in isset() should be
isset($_GET['do'])
not
isset($_GET['do']
Also, you're session is always being set to 0 on each page refresh. You need to only set $_SESSION['test'] = 0 if $_SESSION['test'] has not been set yet.
if (!isset($_SESSION['test'])) {
$_SESSION['test'] = 0;
}
You are missing session_start() at the top of your script. You're also not assigning the session if it doesn't previously exist, you're continuously setting it to 0 before incrementing.
I have a problem with this program of Sessions & Cookies.
Plz see the following code:-
<?php
session_start(); //session starts
if(! isset($_COOKIE['cnt']))
{
#$_SESSION['nv'] = 1;
}
else
{
#$_SESSION['nv'] += 1;
}
$val = $_SESSION['nv'];
echo $val;
setcookie("cnt", $val, time()+30 );
echo "<h1>No. of visits=".#$_COOKIE['cnt'] ."</h1>";
if (#$_COOKIE['cnt'] == 5)
{
setcookie("cnt", 0, time()-30);
session_destroy();
}
?>
It is not giving the correct output.
When I run the program for the first time, it shows:
No. of visits=
Means nothing..
& when I run the program for the second time, it shows:
No. of visits=1
I want that my output should be displayed as "No. of visits=1" when I run the program for the first time. But it shows this output at 2nd time.
Do help me please..
The cookie set by setcookie is sent along with the HTTP response the server sends to the client and, quoting the documentation:
Once the cookies have been set, they can be accessed on the next page
load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.
You should print the session var
echo "<h1>No. of visits=" . $_SESSION['nv'] . "</h1>";
I am looking to set a session variable based on a search conducted by the user. The idea is that the search is populated with their last search wherever they go on the site.
I have the following code that I thought would set the variable if the variable geo-box was present and use the saved variable if it isn't, but this doesn't work...
session_start();
if(isset($_GET['geo-box'])){
echo $_SESSION['town'] = $_GET['geo-box'];
} else {
echo $_SESSION['town'];
}
session_start();
if(isset($_GET['geo-box']))
$_SESSION['town'] = $_GET['geo-box'];
echo $_SESSION['town'];
You can't echo a variable while defining it.
Best of Luck!
You are trying to echo a variable and set it in the same line.
Try this:
session_start();
if( isset($_GET['geo-box']) ) {
$_SESSION['town'] = $_GET['geo-box'];
}
echo $_SESSION['town'];
You can not echo a value and assign it at the same time. Give this a try!
Hope this helps.