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
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?
Related
As you can see, I use this method to store my session,
but sometimes it will not working, but sometimes workings well.
This function is to check cart is exist or not.
protected function Check_Cart() {
$this->cart_no = MDL_Cart::get_session_cart_no($this->company_no, $this->group_buy_no);
if($this->cart_no === '') {
MDL_Cart::Add_new_cart();
}
}
and I will call this funcrion first to check cart is exist in session or not.
protected function Request_Get_Cart_All_Data() {
$check_cart = $this->Check_Cart();
// ...
}
Then I will use this function to get session
protected function Get_Cart_Order_Type() {
$cart_no = MDL_Cart::get_session_cart_no($this->company_no, $this->group_buy_no);
// ...
}
And I use this method to store my session.
public static function Add_new_cart() {
Session::forget('group_cart_data.' . $company_no);
Session::put('group_cart_data.' . $company_no, $cart_no);
Session::save();
}
public static function get_session_cart_no($company_no, $group_buy_no = null) {
if ($group_buy_no) {
return Session::get('group_cart_data.' . $company_no);
} else {
return Session::get('cart_data.' . $company_no);
}
}
When I get session, only Request_Get_Cart_All_Data() has the session, but Get_Cart_Order_Type() cannot get the same session, why?
I'm sure that the session is been writed at Request_Get_Cart_All_Data() because I print the session out to look.
I am practicing PHP, and the issue I encountered today is my static variable getting reset every time I call it.
Login Page after verfied user from DB
<?php
Session::onlogin(1);
?>
This is my session class it has a func onlogin and I send some data to it from another file which change $is_signed to true. But the issue is variable $is_signed getting reset when I call it from index.php it returns false
<?php
class Session {
private static $is_signed = false;
function __construct(){
session_start();
}
public static function is_signed_in(){
return self::$is_signed;
}
public static function onLogin($userid){
if($userid){
session_regenerate_id();
$_SESSION['user_id'] = $userid;
self::$is_signed = true;
return true;
}
}
}
$session = new Session;
?>
Now I am calling it in index.php as below
if(Session::is_signed_in()){
echo "logged In";
}
else {
echo "error";
?>
I don't know where I am creating any error but when I call the methods.
So i asume this
class.php
class Session
{
private static $is_signed = false;
function __construct()
{
session_start();
}
public static function is_signed_in()
{
return self::$is_signed;
}
public static function onLogin($userid)
{
if ($userid) {
session_regenerate_id();
$_SESSION['user_id'] = $userid;
self::$is_signed = true;
return true;
}
}
}
$session = new Session;
login.php
<?php
require 'class.php';
$session = new Session;
Session::onlogin(1);
var_dump(Session::is_signed_in());// output true
index.php
<?php declare(strict_types = 1);
require 'class.php';
var_dump(Session::is_signed_in());// output false
think about it that login.php and index.php are 2 separate programs not aware one of each other.Thats why you try put SESSION there change
function __construct()
{
session_start();
if (isset($_SESSION['user_id'])){
self::$is_signed = true;
}
}
and you are done
I am trying to retrieve a value from one action hook to be displayed in admin page.
public function hookActionProductCancel($params)
{
$this->response = "response";
}
public function hookDisplayAdminOrder($params) {
$this->context->smarty->assign('response', $this->response);
return $this->display(__FILE__, 'views/templates/admin/response.tpl');
}
I am not receiving the value "response" in response.tpl. Probably a small issue but I am not getting it right at the moment.
Any guidance is truly appreciated. Thank you.
You should just store the response to cookie and clear it before displaying it.
public function hookActionProductCancel($params)
{
// code
$this->context->cookie->mymodule_response = "response";
$this->context->cookie->write();
}
public function hookDisplayAdminOrder($params)
{
// if no response stored in cookie do nothing
if (!$this->context->cookie->mymodule_response) {
return false;
}
// assign response from cookie to smarty then clear response from cookie
$this->context->smarty->assign('response', $this->context->cookie->mymodule_response);
unset($this->context->cookie->mymodule_response);
return $this->display(__FILE__, 'views/templates/admin/response.tpl');
}
You are forgetting to use this
public function hookActionProductCancel($params)
{
..... codes .....
$this->response = "response";
}
public function hookDisplayAdminOrder($params)
{
$this->context->smarty->assign('response', $this->response);
return $this->display(__FILE__, 'views/templates/admin/response.tpl');
}
I have this session class
class Session
{
public function __construct()
{
if (!isset($_SESSION)) {
$this->initSession();
}
}
public function setSessionId()
{
$_SESSION['Id'] = session_id();
}
public function getSessionId()
{
return $_SESSION['Id'];
}
public function initSession()
{
session_start();
error_reporting(E_ALL);
$this->setSessionId();
}
public function isLogged(){
if(isset($_SESSION['Id'])){
return true;
}else{
$this->deleteSession();
return false;
}
}
In my login.php I pass the id to my session but
$results['Id'] = session_id();
when I got to index.php and echo the Id it echos a long string not the current user Id
$session = new Session();
$id = $session->getSessionId();
echo $id;
Please help me how do I pass the variable!I have tried a lot but I seem to be stuck here!Would really appreciate some help
Add additional methods to your class:
public function setUserId($userid) {
$_SESSION['user_id'] = $userid;
}
public function getUserId() {
return isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
}
Then your login page should call $session->setUserId() after the user logs in, passing the ID from the database.
I am trying to set a php session from my loginpage:
if($results){
$session->setId($results['Id']);
}
In my session class I have these:
class Session{
public function createSession(){
session_start();
error_reporting(E_ALL);
}
public function getId(){
return $_SESSION['Id'];
}
public function setId($value){
$_SESSION['Id'] = $value;
}
}
Then I try to call it to the index my Id
$thisSession=Session::getId();
if(isset($thisSession)) {
echo "The session is set.";
} else{
echo "Sorry, it's not set.";
}
How can I do it?
Undefined index: Id
To pass the session id from login to my index page!
You can for sure create an object that encapsulate the $_SESSION but be careful about how PHP works.
I suggest small modifications to your class (if it's ok to you) using the session_id method :
class Session
{
public function __construct()
{
if (!isset($_SESSION)) {
$this->initSession();
}
}
protected function initSession()
{
session_start();
error_reporting(E_ALL);
$this->setSessionId();
}
public function setSessionId()
{
$_SESSION['Id'] = session_id();
}
public function getSessionId()
{
return $_SESSION['Id'];
}
}
Keep in mind that $_SESSION variables are accessible by users easily don't put in it too much informations, especially sensitive.
$_SESSION is a PHP superglobal variable you can retrieve it wherever your want to, but you can get it by declare :
$session = new Session();
$id = $session->getSessionId();
var_dump($id);
For logout you should add this method :
public function deleteSession()
{
session_destroy();
}
And then use when disconnected action is asked
$session = new Session();
$session->deleteSession();