How to access increment value of session on same page in php - php

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'].

Related

Variable should not return to default value

<?php
$transVar = "000000001";
$transF = (1+ $transVar);
echo "<br>";
echo $transF;
?>
I need the output of the code to be 0000000002 and be incrementing on refresh even closing and reloading the script, I don't seem to be getting it, Any help?
This should do the trick:
if (isset($_SESSION["TransVar"])) {
$_SESSION["TransVar"] += 1;
} else {
$_SESSION["TransVar"] = 1;
}
echo str_pad($_SESSION["TransVar"], 8, '0', STR_PAD_LEFT);
When the page loads the first time we set the session value to 1, then increment it after each page refresh. We keep the session value as a number, and pad it with zeroes instead of saving the value as a string.
Keep in mind that this value is only persisted as long as the session is alive, so it won't be stored forever. If you need to persist this value indefinetely, then you need a different approach.
Read more about PHP sessions: https://www.php.net/manual/en/intro.session.php

PHP Session Variable - change on page load

I'm trying to get a session variable to alternate between 0 and 1 on each page load.
So first time page loads
$_SESSION['turn'] = 0;
Second time
$_SESSION['turn'] = 1;
Third time
`$_SESSION['turn'] = 0;`
and so on.
Then I can call that variable later in the page.
I can't work out how to do this. I've tried a simple IF function but can't get it to work.
First the session must be started on any page wishing to make use of the session array. session_start()
Next you have to remember that initially the session variable you are using will not exist the first time you attempt to use it
So
<?php
session_start();
if ( !isset($_SESSION['turn']) ) {
// does not exist yet, so create with 0
// you may want to initialize it to 1, thats up to you
$_SESSION['turn'] = 0;
} else {
$_SESSION['turn'] = $_SESSION['turn'] == 0 ? 1 : 0;
}
Try this where the page is loaded.
$_SESSION['turn']=1-$_SESSION['turn'];
code:
<?php
session_start();
echo $_SESSION['turn'];
$_SESSION['turn']=1-$_SESSION['turn'];
?>
Edit : RiggsFolly !isset() is correct. mine misses it and it will give errors in log. and the first value is not 0

php session variable value destroyed after reaload page in wordpress

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

PHP- Value of SESSION variable not echoing properly when page is refreshed

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.

php session problem

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.

Categories