Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

php - Session won't persist on reload in magento

I am having trouble with reloading a page in magento and not being able to hold a session variable.

Here are some code snippets from my project

view.phtml

 <?php
 //Get the session object
 $session = Mage::getSingleton("core/session", array("name"=>"frontend"));  
 $groupid = $session->getMyGroupId("myGroupID");
 echo $groupid;

This is at the top of my view page. There is a drop down menu that with a submit button that goes off to another page called other.php - which takes the value of the drop down and puts it into a session variable

<?php 
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

//Set groupid
$data = $_POST['county_select'];
$session = Mage::getSingleton("core/session", array("name"=>"frontend"));
$session->setMyGroupId($data);

header('Location: '.$_SERVER["HTTP_REFERER"]);
?>

This redirects back to the page I was on with the drop down menu and the correct value is echoed out on screen.

Now, if I just refresh this page, the session variable disappears. It's as if it has been unset. Can anyone spot what I am doing wrong please? If I choose from the dropdown menu again, everything comes back fine as before, but a straight refresh means nothing gets echoed out for groupid so I know the session variable is no good now.

Thanks. DS

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

replace view.phtml code with this:

echo Mage::getSingleton("core/session", array("name"=>"frontend"))->getMyGroupId();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...