php session variable value destroyed after reaload page in wordpress - php

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

Related

How to access increment value of session on same page in 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'].

setcookie(); does not set cookies

I have following problem, I am trying to set cookies without any success. setcookie(); function returns true so it looks like it setting cookie however when I am trying to access it on the same or following page I get error 'Undefined Index....'
<?
session_start();
ob_start();
echo setcookie("order",$_SESSION['cart'],time()+3600,'/',NULL);
//added to see if Cookie is set
echo "<br/>";
var_dump($_COOKIE);
exit();
if($_GET['paypal'] == 1){
header("Location: /paypal-express-checkout/process.php");
}else{
header("Location: /insert_order.php");
}
ob_end_flush();
exit();
?>
next page follows like this
<?php
session_start();
include_once("../includes/inc_config.php");
include_once("../order.php");
include_once("config.php");
include_once("paypal.class.php");
#region POST
if(!isset($_GET['token'])) //Post Data received from product list page.
{
//Mainly we need 4 variables from an item, Item Name, Item Price, Item Number and Item Quantity.
if(!isset($_COOKIE['order'])){
exit();
}
$paypal_data = '';
$ItemTotalPrice = 0;
$order = unserialize($_COOKIE['order']);
print_r($order);
exit;
You are setting the domain value to NULL. Try leaving the NULL away:
echo setcookie("order",$_SESSION['cart'],time()+3600,'/');
OR set it to your domain:
echo setcookie("order",$_SESSION['cart'],time()+3600,'/',".yourdomain.com");
I would var_dump or print_r the $_COOKIE variable before I make a decision that it's not getting passed through. Holding that thought once you setcookie something gets registered into the $_COOKIE variable for sure.
I agree with the statements above since you only can access $_COOKIE on the next refresh but there is another way to do it to make your form or page more interactive.
I would register the cookie and use a php page refresh (display a working... div while thats happening) then come back to the page and try to do what you originally tried doing. Very basic but pretty much straight forward.

Only change session variable if GET request has value

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.

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.

How do I save value in my own session variable in Magento?

I am using Magento and trying to save a value in the session as follows in its index.php file, but the value is not being retained.
$_SESSION['myvar'] = '1';
How do I do it?
Thanks
Let's say you want to save the value "Hello world" to the "welcome message" variable in the session. The code would be :
$inputMessage = 'Hello World';
Mage::getSingleton('core/session')->setWelcomeMessage($inputMessage);
Now you want to echo the "welcome message" somewhere else in your code/site.
$outputMessage = Mage::getSingleton('core/session')->getWelcomeMessage();
echo $this->__($outputMessage);
Following the example given by Ali Nasrullah, I would do:
$session = Mage::getSingleton("core/session", array("name"=>"frontend"));
// set data
$session->setData("device_id", 4);
// get data
$myDeviceId = $session->getData("device_id");
Make sure you include [Mage-root]/app/Mage.php befor calling the code above!
#Ali Nasrullah: Pass the value of device:id as second parameter of the setData function.
Mage::getSingleton('core/session')->setMySessionVariable('MyValue');
$myValue = Mage::getSingleton('core/session')->getMySessionVariable();
echo $myValue;
Take Look For More:
Here are code to Get, Set, and Unset Session in Magento
Here are code to Get, Set, and Unset Session in Magento
frontend: Mage::getSingleton('core/session')->setYourNameSession($session_value);
backend: Mage::getSingleton('admin/session')->setYourNameSession($session_value);

Categories