php session variable is null - php

i am creating session variables to store data in an array of object. this array is assigned to sessions. I am later sending a get call to different page with an id and want to access the corresponding data from the sessions. however i am getting the data as null. here is my code
page 1:
session_start();
for ($i=0;$i<100;$i++){
$object[$i]->name = $ret_obj[$i]['name'];
$object[$i]->birthday_date = $ret_obj[$i]['birthday_date'];
$_SESSION[$i] = $object[$i];
}
var_dump of session prints the session variable correctly.
Now in the for loop i am making a call to page 2:
page2.php?pid=$i
page 2:
session_start();
$pid = $_GET['pid'];
print_r($_SESSION[$pid]);
print_r($_SESSION);
I am getting value in $_SESSION but not in $_SESSION[$pid]

You should take a look at the following post: Notice: Unknown: Skipping numeric key 1 in Unknown on line 0. To clarify, try adding a character prefix instead of just using numbers.

If your code supplied here is all of it, then you are saying:
$p13nid = $_GET['pid'];
Rather than:
$pid = $_GET['pid'];
Which would make it work for you.

Related

Passing a multi-dimensional array using sessions in PHP

I am currently trying to pass a multidimensional array through sessions while also being able to dynamically add/remove from it (it is a wish-list). The index of the array will be the ID of an item I am adding to the array. I've tried serialization, using variables instead of the actual session and none of it worked properly for me.
My issue is that my array will not pass from page 1 to page 2. Page 1 is what happens when the user clicks any "add to wish-list" button
I searched up on Google and wrote something similar to this:
page 1:
session_start();
$_SESSION['wishlist'] = array();
$id = $_GET['id'];
$imageFileName = $_GET['ImageFileName'];
$title = urldecode($_GET['PictureName']);
$_SESSION['wishlist'][$id]=array("id"=>$id,"title"=>$title,"imageFileName"=>$imageFileName); // Here im making the multidimensional array
$_SESSION['wishlist'] = $_POST; //found this way on Stackoverflow
header('Location:view-wish-list.php'); //move to second page
page 2: trying to start session and print out the array to test:
session_start();
var_dump($_SESSION['wishlist']);
Var Dump gives me array(0) { }
Any help would be appreciated. Thank you.
You have to commit (write) the session before redirection or the second request may occur before the session data is available on the server:
session_write_close();
header('Location:view-wish-list.php'); //move to second page

Array using a post and a session variable

Hello guys I am trying to insert a post_variable into an array and send it with a session variable to a different file, my code for adding the post variable is below:
$sit = array();
$sit[]= $_POST['sit'];
$_SESSION['sit'] = $sit;
when I transfer the variable to a different file(customerform.php), I am trying to echo one of the strings into the array using the code below:
session_start();
cart =_SESSION['sit'];
is_null($_SESSION['sit'])
;
echo $cart[1];
I tried many things but it keeps displaying the following error on my screen:
"Undefined offset: 1 in C:\xampp\htdocs\theater\customerform.php on
line 9"
The issue is that you are attempting to access $cart[1], when $cart[1] doesn't exist.
When you add an item to an array without specifying an index, the default beginning index is 0.
So, to access your value, you'll want to use $cart[0];
If you add a second item to your array in the same manner that you did before ($sit[]= $_POST['sit']), it will be placed at $cart[1] unless you specify a different index.
I also realized that you haven't properly set your value here:
cart =_SESSION['sit'];
It should be:
$cart = $_SESSION['sit'];

How to unserialize codeigniter session data from database

I wan to use CI session in a external script and I got following data from database.
__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example#xyz.com";logged_in|b:1;
I have tried unserialize and unserialize(base64_decode($data)) but I am fail yet.
Please help to extract this data.
I got the solution here
So I have used session decode
session_decode('__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example#xyz.com";logged_in|b:1;');
So session decode stored all the encrypted data in normal php session.
Which I can access using: echo $_SESSION['ci_UserID'];
Well guys thanks for the help
If this is a session variable, you can use CodeIgniter's own session library. Consider the following code (in a controller):
$this->load->library('session'); // load the session library
$session_data = $this->session->all_userdata(); // get all session data
print_r($session_data); // print and get the corrresponding variable name, e.g. "item"
$var = $this->session->userdata('item'); // pick one that suits your needs, e.g. item
Sorry, I have read "the external script" only after having posted the code. This obviously only works in the CI framework.
For an external script you may need to have a closer look. The variables are separated by ";" and "|" and then serialized, so this might work (not tested):
$row = explode(';', '__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example#xyz.com";logged_in|b:1;'); // load the database row
$userid = explode('|', $row[1]);
$userid = unserialize($userid[1]); // now $userid holds the value "2"

how to add an array to a session php

I want to add different stuffs to costumers cart but before adding the stuff transition in the database costumer must pay and then redirect to another page after Successful transition i need the chosen stuffs id's i tried using $_POST but the browser does not send the post value because of payment system in the middle i tried using sessions instead
I want to add array of integers to a session control i already tried using this code
$t=array(1,2,3,4,5);
$_SESSION['exam_id']=$t
I don't know if i can do such a thing or not but what is the parallels
What you specified is working correctly. Sessions can hold arrays.
The session superglobal is an represented as an array itself within PHP, so you can just get and set values by doing the following
Setting:
$_SESSION['exam_id'][] = "new value";
or
$_SESSION['exam_id'] = array("new value", "another value");
Getting:
$_SESSION['exam_id'][0]; // returns a value
This returns the first value in the exam_id array within the session variable
You will need to start the session. Make your code;
<?php
session_start();
$t = array(1,2,3,4,5);
$_SESSION['exam_id'] = $t;
You need session_start()
You didn't have a semi-colon ; at the end.
you can use array in session like this..
you have to start session with session_start();
and then store your array to session $_SESSION['items'][] = $item;
and then you use it, whenever you want:
foreach($_SESSION['items'][] as $item)
{
echo $item;
}
You can set PHP sessions equal to an array just like you're doing.
To set the $_SESSION variable equal to all POST data, you can do this:
$_SESSION['exam_id'] = $_POST;
Be sure to add session_start() prior to declaring any session variables.
$t=array(1,2,3,4,5);
$_SESSION['exam_id'] = array();
array_push($_SESSION['exam_id'],$t[0]);
array_push($_SESSION['exam_id'],$t[1]);
array_push($_SESSION['exam_id'],$t[2]);
array_push($_SESSION['exam_id'],$t[3]);
array_push($_SESSION['exam_id'],$t[4]);

URL variable to Session variable (using GET method)

I am trying to store a value obtained from a URL variable into a SESSION variable.
Here is the URL:
Ace Hardware
Here is the SESSION coding, which retrieves the variable, but loses the variable value upon leaving the page.
$_SESSION["store"] = $_GET["store"];
$shopByStore = $_SESSION["store"];
If I plug in the value in quotes as it is below (see "Ace" in code below), it works. But it doesn't work in the code above using the GET method ($_GET["store"])
$_SESSION["store"] = "Ace";
$shopByStore = $_SESSION["store"];
The problem is that you override the $_SESSION["store"] with the $_GET["store"] each time whether the get request exists or not, so it basically only uses $_GET["store"]. You could use this instead:
if (isset($_GET["store"])) {
$_SESSION["store"] = $_GET["store"];
}
At first you need to start the session and then make sure it's already not stored in the session (if it's passed in the $_GET) and if not already saved in the session then store it:
// The first line in your script
session_start();
if (isset($_GET["store"])) {
if (!isset($_SESSION["store"])) {
$_SESSION["store"] = $_GET["store"];
}
}
Read more on PHP manual.

Categories