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

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);

Related

How to pass variable to another page without include

Hey guys I'm trying to pass a php variable to another page. I tried it with sessions but no result.
newspaper.php
$newspaper= $newspaper['newspath'];
print_r($newspaper);
this outputs:
path/to/the/newspaper.
Now I want to use the variable in the second page.
newspaperviewer.php
echo $newspaper;
$SESSION = $newspaper;
I tried the first one but no result. The second one seems to be faulty.
Hope you guys can help me out.
Session is what you are looking for. A session variable can store a value and use this value on all pages of your project.
First thing to do is to start session on each file on your project. You can do this like this example
<?php
session_start(); //declare you are starting a session
$_SESSION['newspaper'] = "New York Times"; //Assign a value to the newspaper session
?>
On the other file you can use the value of the session by trying something like this
<?php
session_start(); //always start session don't forget!!
echo $_SESSION['newspaper'];
// This will echo New York Times
?>
Store the variable after starting session on page A, like so:
// FIRST PAGE (foo.php)
session_start();
$_SESSION['name'] = 'Jack';
Now, on the second page (or any page that you want to have access to $_SESSION, simply do the same but pull the variable.
// SECOND PAGE (bar.php)
session_start();
$name = $_SESSION['name'];
$_SESSION['name'] = null; // Or use session_unset() to delete all SESSION vars.
And that's how you pass variables using $_SESSION.
Please use this code to set session
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
You can write like this
newspaper.php
session_start();
$newspaper= $newspaper['newspath'];
$_SESSION['newspaper'] = $newspaper;
Now you can use this session variable in
newspaperviewer.php
session_start();
$newspaper = $_SESSION['newspaper'];
echo $newspaper;
session_unset(); // remove all session variables
First
newspaper.php
$newspaper= $newspaper['newspath'];
//print_r($newspaper);
session_start(); //it starts your session here
$_SESSION['newspaper']=$newspaper; //it sets a session variable named as newspaper
Second
$newspaper= isset($_SESSION['newspaper'])?$_SESSION['newspaper']:''; //checks and sets value
echo $newspaper; //outputs value
For more see session_start
http://php.net/manual/en/session.examples.basic.php
First you will need to start the session by using session_start() at the top of your page. Second, a session variable is written like this: $_SESSION['foo'].
I suggest you read these pages to get a better understanding of what's going on.
http://php.net/manual/en/reserved.variables.session.php
http://www.w3schools.com/php/php_sessions.asp

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

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.

Pass variables between two PHP pages without using a form or the URL of page

I want to pass a couple of variables from one PHP page to another. I am not using a form. The variables are some messages that the target page will display if something goes wrong. How can I pass these variables to the other PHP page while keeping them invisible?
e.g. let's say that I have these two variables:
//Original page
$message1 = "A message";
$message2 = "Another message";
and I want to pass them from page1.php to page2.php. I don't want to pass them through the URL.
//I don't want
'page2.php?message='.$message1.'&message2='.$message2
Is there a way (maybe through $_POST?) to send the variables? If anyone is wondering why I want them to be invisible, I just don't want a big URL address with parameters like "&message=Problem while uploading your file. This is not a valid .zip file" and I don't have much time to change the redirections of my page to avoid this problem.
Sessions would be good choice for you. Take a look at these two examples from PHP Manual:
Code of page1.php
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br />page 2';
// Or pass along the session id, if needed
echo '<br />page 2';
?>
Code of page2.php
<?php
// page2.php
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
// You may want to use SID here, like we did in page1.php
echo '<br />page 1';
?>
To clear up things - SID is PHP's predefined constant which contains session name and its id. Example SID value:
PHPSESSID=d78d0851898450eb6aa1e6b1d2a484f1
Here are brief list:
JQuery with JSON stuff. (http://www.w3schools.com/xml/xml_http.asp)
$_SESSION - probably best way
Custom cookie - will not *always* work.
HTTP headers - some proxy can block it.
database such MySQL, Postgres or something else such Redis or Memcached (e.g. similar to home-made session, "locked" by IP address)
APC - similar to database, will not *always* work.
HTTP_REFERRER
URL hash parameter , e.g. http://domain.com/page.php#param - you will need some JavaScript to collect the hash. - gmail heavy use this.
<?php
session_start();
$message1 = "A message";
$message2 = "Another message";
$_SESSION['firstMessage'] = $message1;
$_SESSION['secondMessage'] = $message2;
?>
Stores the sessions on page 1 then on page 2 do
<?php
session_start();
echo $_SESSION['firstMessage'];
echo $_SESSION['secondMessage'];
?>
Have you tried adding both to $_SESSION?
Then at the top of your page2.php just add:
<?php
session_start();
Use Sessions.
Page1:
session_start();
$_SESSION['message'] = "Some message"
Page2:
session_start();
var_dump($_SESSION['message']);
In MVC, you can pass variable one page to another like this:
<?php $this->load->view('Overview', ['customer' => $customer , 'job_id' => $job_id , 'email' => $emailid]);?>
In Overview.php page you can display variable data like this
echo $customer; // it will display customer value
echo $job_id;
echo $email; // it will display email id

SESSION problem when using GET method in php

<?php session_start();?>
send the color
<br><br>
<?php
#$_SESSION['color'] = $_GET['color'];
echo $_SESSION['color'];
?>
<br>
<br>
check the session variable
hi,
i need help for the above code.
i want to pass a variable to session.
with above code im doing this but the session variable dissappears when i refresh the page or when i click the bottom link. i want the echo $_SESSION['color']; sticky
what should i do?
with regards
You are assigning value of $_GET['color'] no matter if there is such GET variable or not. Because of this, when there is no $_GET['color'] you are loosing the session variable.
it have to be:
if (isset($_GET['color'])) {
$_SESSION['color'] = $_GET['color'];
}
echo isset($_SESSION['color']) ? $_SESSION['color'] : '';
When $_GET['color'] is empty because color is not in the query string of the URL, you still assign that empty value to $_SESSION['color'].
Don't do that and the value you set won't be overwritten. Nothing was disappearing on its own.

Categories