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.
Related
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();
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?
I have this simple session class
class Session
{
public static function init() {
#session_start();
}
public static function set($key, $value) {
$_SESSION[$key] = $value;
}
public static function get($key) {
if (isset($_SESSION[$key]))
return $_SESSION[$key];
}
public static function destroy() {
unset($_SESSION);
session_destroy();
}
}
In my other class I have
public function verifyFormToken($form)
{
// check if a session is started and a token is transmitted, if not return an error
if(!isset(Session::get($form.'_token'))){
return false;
}
// check if the form is sent with token in it
if(!isset($data['token'])) {
return false;
}
// compare the tokens against each other if they are still the same
if (Session::get($form.'_token') !== $data['token']) {
return false;
}
return true;
}
I can set a session no problem but when I come to get it using verifyFormToken(), i get this error message
Can't use function return value in write context
which points to this line
if(!isset(Session::get($form.'_token'))){
you will have to define a variable as pass that to isset:
$token = Session::get($form.'_token');
if ($token !== NULL) { ... }
or as you are using isset in your get method just do:
if (Session::get($form.'_token') !== NULL) { ... }
EDIT**
In this instance this would be fine, as session token will never be null, but as a session controller, a value may be set as NULL on purpose, or may not be set, so your get method needs to return a unique value to determine whether its set or not. i.e.
define('MY_SESSION_NOT_SET',md5('value_not_set'));
class Session
{
public static function init() {
#session_start();
}
public static function set($key, $value) {
$_SESSION[$key] = $value;
}
public static function get($key) {
if (isset($_SESSION[$key]))
return $_SESSION[$key];
else
return MY_SESSION_NOT_SET;
}
public static function destroy() {
unset($_SESSION);
session_destroy();
}
}
if (Session::get($form.'_token') === MY_SESSION_NOT_SET) { ... }
something like that would be more beneficial.
I searched, and found this question, which helped me:
php static variable is not getting set
It did, however, not solve my entire problem.
Code:
Class DummyClass {
public static $result;
function __construct() {
$this->_setResultCode('testing');
}
public function getResultCode() {
return self::$result['code'];
}
private function _setResultCode($val) {
echo 'Im gonna set it to: ' . $val . '<br />';
self::$result['code'] = $val;
echo 'I just set it to: ' . $this->getResultCode;
die();
}
}
Outputs:
Im gonna set it to: testing
I just set it to:
What's going on here? How is this even possible?
EDIT: The problem was i missed the parentheses when calling getResultCode(). HOWEVER, i have another issue now. I can't seem to get the resultCode out of the class (later on in another instance of DummyClass).
Here is my relevant coded (No more example code because i seemed to mess that up):
Class lightweightContactFormPlugin {
// Set up/Init static $result variable
public static $result;
function __construct() {
//echo 'I just inited<br/><pre>';
//var_dump($this->getResultCode());
//echo '</pre><br/>';
}
public function run() {
// Set default value for resultCode
$this->_setResultCode('no_identifier');
// Check if form was posted
if(isset($_POST['cfidentifier'])) {
$fields = $this->_get_fields_to_send();
$valid = $this->_validate_fields($fields);
// Only continue if validatation was successful
if($valid == true) {
// Store mail result in $mail
$mail = $this->_send_mail($fields);
// Yay, success!
if($mail) {
$this->_setResultCode('sent_successfully');
return;
} else {
// Couldn't send mail, bu-hu!
$this->_setResultCode('not_sent');
return;
}
}
$this->_setResultCode('validation_fail');
return;
}
}
// Get and Set methods
public function getResultCode() {
return isset(self::$result['code']) ? self::$result['code'] : '';
}
private function _setResultCode($val) {
self::$result['code'] = $val;
}
}
Left some irrelevant methods out. None of the other methods set or get the resultCode, it shouldn't matter.
Any ideas why i can't access $result['code'] in another instance of the object (further down the page)?
I do this when i access it:
$plugin = new lightweightContactFormPlugin();
$cfstat = $plugin->getResultCode();
echo '<pre>';
var_dump($fstat);
echo '</pre>';
Result is:
NULL
The strange thing is, if i uncomment the code in __construct(), the CORRECT value does get printed out! But if i try to access it from getResultCode() after, it returns NULL again. What is going on?
echo 'I just set it to: ' . $this->getResultCode;
I think you're missing a few parenthesis here.
You have no return within getResultCode()
You have to call getResultCode() as a method.
echo 'I just set it to: ' . $this->getResultCode();
A. your code is wrong ... there is nothing like __constructor in PHP it should be
function __construct() {
B. Your code should also return the following since getResultCode was not set
Notice: Undefined property: DummyClass::$getResultCode
You should be calling
echo 'I just set it to: ' . $this->getResultCode();
Your Final Code :
class DummyClass {
public static $result;
function __construct() {
$this->_setResultCode('testing');
}
public function getResultCode() {
return self::$result['code'];
}
private function _setResultCode($val) {
echo 'Im gonna set it to: ' . $val . '<br />';
self::$result['code'] = $val;
echo 'I just set it to: ' . $this->getResultCode();
die();
}
}
new DummyClass();
Output
Im gonna set it to: testing
I just set it to: testing
use return like this
public function getResultCode() {
return self::$result['code'];
}
and use $this->getResultCode();
EDIT
the only problem i can see is you have just written return; , because of that it return NULL, change it to
return $this->getResultCode();
Basically, what I want to do is create a class called Variables that uses sessions to store everything in it, allowing me to quickly get and store data that needs to be used throughout the entire site without working directly with sessions.
Right now, my code looks like this:
<?php
class Variables
{
public function __construct()
{
if(session_id() === "")
{
session_start();
}
}
public function __set($name,$value)
{
$_SESSION["Variables"][$name] = $value;
}
public function __get($name)
{
return $_SESSION["Variables"][$name];
}
public function __isset($name)
{
return isset($_SESSION["Variables"][$name]);
}
}
However, when I try to use it like a natural variable, for example...
$tpl = new Variables;
$tpl->test[2] = Moo;
echo($tpl->test[2]);
I end up getting "o" instead of "Moo" as it sets test to be "Moo," completely ignoring the array. I know I can work around it by doing
$tpl->test = array("Test","Test","Moo");
echo($tpl->test[2]);
but I would like to be able to use it as if it was a natural variable. Is this possible?
You'll want to make __get return by reference:
<?php
class Variables
{
public function __construct()
{
if(session_id() === "")
{
session_start();
}
}
public function __set($name,$value)
{
$_SESSION["Variables"][$name] = $value;
}
public function &__get($name)
{
return $_SESSION["Variables"][$name];
}
public function __isset($name)
{
return isset($_SESSION["Variables"][$name]);
}
}
$tpl = new Variables;
$tpl->test[2] = "Moo";
echo($tpl->test[2]);
Gives "Moo".