我想获得一个会话,然后返回其数据然后取消设置(或获取一个会话,然后取消设置然后返回数据),我正在做2(头)重定向。 在设置会话直到获得会话之后。
但问题是它没有返回任何东西。
以下是我正在使用的课程:
班级会议{
private $session_data1; public function __construct() { session_start(); // starts session on all files at first... } public function set_session($session_name, $session_data) { return ($_SESSION[$session_name] = $session_data) ? true : false; } public function get_session($session_name) { return isset($_SESSION[$session_name]) ? $_SESSION[$session_name] : false; } public function get_session_once($session_name) { $this->session_data1 = $this->get_session($session_name); $this->unset_session($session_name); return $this->session_data1; } public function unset_session($session_name) { if (isset($_SESSION[$session_name])) { unset($_SESSION[$session_name]); // return true; } } public function destroy_all_session() { session_destroy(); }}
$ session = new Session();
我正在使用'set_session()'来设置会话,并希望使用'get_session_once()'来取消会话,然后返回该会话的值。
如果我不在方法'get_session_once()'中取消它,那么它可以工作,如下所示:
public function get_session_once($session_name) { $this->session_data1 = $this->get_session($session_name); // $this->unset_session($session_name); return $this->session_data1; }我是PHP新手,请帮忙
I want to get a session and then return its data then unset it (or get a session and then unset it then return the data), I am doing 2 (header) redirects. after setting the session till getting the session.
but the problem is its not returning anything.
following is the class I am using:
class Session {
private $session_data1; public function __construct() { session_start(); // starts session on all files at first... } public function set_session($session_name, $session_data) { return ($_SESSION[$session_name] = $session_data) ? true : false; } public function get_session($session_name) { return isset($_SESSION[$session_name]) ? $_SESSION[$session_name] : false; } public function get_session_once($session_name) { $this->session_data1 = $this->get_session($session_name); $this->unset_session($session_name); return $this->session_data1; } public function unset_session($session_name) { if (isset($_SESSION[$session_name])) { unset($_SESSION[$session_name]); // return true; } } public function destroy_all_session() { session_destroy(); }}
$session = new Session();
I am using 'set_session()' to set a session and wanted to use 'get_session_once()' which will unset the session and then return the value of that session.
if I dont unset it in the method 'get_session_once()' then it works, like the following:
public function get_session_once($session_name) { $this->session_data1 = $this->get_session($session_name); // $this->unset_session($session_name); return $this->session_data1; }I am new in PHP, please help
最满意答案
我对此进行了测试,对我来说这是有效的。
<? $session = new Session(); $session->set_session('Username', 'StackOverflow'); echo $session->get_session_once('Username'); // echos 'StackOverflow' echo $session->get_session_once('Username'); // no echo ?>这仍然是一样的:
public function get_session_once($session_name) { $this->session_data1 = $this->get_session($session_name); $this->unset_session($session_name); return $this->session_data1; }也许标题重定向是原因,它可能是清除/删除会话。
你把session_start()放在每个页面的开头吗?
I tested this, and for me it's working.
<? $session = new Session(); $session->set_session('Username', 'StackOverflow'); echo $session->get_session_once('Username'); // echos 'StackOverflow' echo $session->get_session_once('Username'); // no echo ?>This is still the same:
public function get_session_once($session_name) { $this->session_data1 = $this->get_session($session_name); $this->unset_session($session_name); return $this->session_data1; }Maybe the header redirect is the cause, it might be clear/delete the session.
Did you put session_start() at the start of every page?
更多推荐
发布评论