Set and get sessions - php

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

Related

Laravel session got lost

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.

Class static variables reset when I call them in other file

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

Session not getting the variable prints long string

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.

unset a session then return its data

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?

set session in database in php

How can I use session in database table with php and mysql?
You would need to create an object like so:
class SessionHandler
{
private static $lifetime = 0;
private function __construct() //object constructor
{
session_set_save_handler(
array($this,'open'),
array($this,'close'),
array($this,'read'),
array($this,'write'),
array($this,'destroy'),
array($this,'gc')
);
}
public function start($session_name = null)
{
session_start($session_name); //Start it here
}
public static function open()
{
//Connect to mysql, if already connected, check the connection state here.
return true;
}
public static function read($id)
{
//Get data from DB with id = $id;
}
public static function write($id, $data)
{
//insert data to DB, take note of serialize
}
public static function destroy($id)
{
//MySql delete sessions where ID = $id
}
public static function gc()
{
return true;
}
public static function close()
{
return true;
}
public function __destruct()
{
session_write_close();
}
}
Then before session_start initiate this class!
include 'classes/sessionHandlerDB.php';
$session = new SessionHandler();
$session->start('userbase');
$_SESSION['name'] = 'Robert Pitt'; //This is sent to SessionHandler::write('my_id','Robert Pitt')
echo $_SESSION['name']; //This calls SessionHandler::read($id)//$id is Unique Identifier for that
http://php.net/manual/en/function.session-set-save-handler.php
http://php.net/manual/en/function.serialize.php
You control this in php.ini under the session_handler directive. Check out http://www.tonymarston.net/php-mysql/session-handler.html for a easy walk through (used it before).
You'll need to usesession_set_save_handler to write custom open, close, read, write, destroy, and garbage collection functions.
See my github code snippet PHP5.4-DB-Session-Handler-Class for a database driven session management class in PHP 5.4.

Categories