Session array not unset - php

I want to unset session array but It is not happening.
Where am I wrong in this?
if(isset($_SESSION['id']) && isset($_POST['processorder']))
{
$chk = $_SESSION['id'];
$query="update order_details set process_order='1' where id IN(".implode(',',$chk).")";
mysql_query($query) or die(mysql_error());
unset($chk);
}

$chk = $_SESSION['id'];
What you're doing here is creating a variable $chk with the value $_SESSION['id'] then unsetting this $chk but you've never touched the $_SESSION var.
To do so you need to have the following code :
$_SESSION['id'] = '';
unset($_SESSION['id']);

use php unset like that :-
unset($_SESSION['id']);
unset($_SESSION['processorder']);
or you can use session_destroy()
will delete ALL data associated with that user.
Not this unset($chk);

$chk is just a reference varialble. You need to unset the original variable.
unset($_SESSION['id']);

Use NULL and unset to remove the saved values in $_SESSION[] variables, like:
$_SESSION['id'] = NULL;
unset($_SESSION['id']);
If you put
$_SESSION['id'] = '';
and in case there is space in between '' two quotes, then
if(isset($_SESSION['id'])){}
returns true instead of false.
NOTE: You can also use session_destroy() function, but it destroy all saved session data.

Unsetting $chk doesn't actually affect the original array. You could use references to do that. But keep in mind that unsetting a reference itself doesn't affect the original array either. This should work:
$session = &$_SESSION;
unset($session[id']);
This could be useful if you need to perform a number of actions on the $_SESSION - should make your code a bit more readable

Related

How to combine the $_SESSION into one variable in PHP mysqli ?

normally, i use the single $_SESSION for single field in the table as below in mysqli, let's say i use 'email' field in users table
$email = mysqli_real_escape_string($connect,$_POST["email"]);
$_SESSION['email'] =$email;
but it only can use email as sessions right ?
But what if i have multiple fields in the users table such as username, password, gender ?. How to combine/bind all the $_SESSION fields together into one variable ?
$user = $sessions_variable['username'];
thanks in advance :)
$_SESSON is an array. So your first move is to read up on arrays in the PHP manual.
After that you'll be able to either assign different values to different $_SESSON elements
$_SESSION['email'] = $email;
$_SESSION['username'] = $username;
or, after getting the user info from a database, assign it to a single element
$_SESSION['user'] = $row;
You can create multidimensional array in session like this
$_SESSION['user']['username'] =$username;
$_SESSION['user']['email'] =$email;
then you can pass whole user session array to single variable.
$user = $_SESSION['user'];
We can create multidimensional array for session follow the below procedure.
Example:
$_SESSION['foo']['foo_1'] =$foo1;
$_SESSION['foo']['foo_2'] =$foo2;
$_SESSION['foo']['foo_3'] =$foo3;
...
Now if we want to use whole foo array in single variable. you have to pass it like this.
$foo_whole_session = $_SESSION['foo'];
You can check what you found using print_r($_SESSION).

Storing all POST data in SESSION

I have more values (over 20) from $_POST like this...
$name = $_POST['name'];
$username = $_POST['username'];
$city = $_POST['city'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
NOTE : I have prevented there values from SQL injection.
My question is can I know is there a way to store all these POST values in SESSION at once?
Instead of this method..
$_SESSION['name'] = $name; etc
any answers will be highly appreciated.
Thank you.
$_SESSION['post-data'] = $_POST;
That will create an array of the post values in a single session variable. The values would be accessible as
$_SESSION['post-data']['name']
$_SESSION['post-data']['username']
...
You can add one array to another.
$_POST and $_SESSION are just arrays.
Note that the keys in the second array take priority in case of duplicates.
$_SESSION += $_POST;
However, I don't see this ending well, since the client side can inject anything he wants to the session, E.G. hijacking your users session id.
If you looking for session variables that are set using the same key as the posted array, you could use this:
foreach ($_POST as $key => $value) {
${$key} = $value;
$_SESSION[$key] = $value;
}
This will make your $_POST array into variables based on the array key and also set the session at the same time. You could then access your name post by using either $_SESSION['name'] or $name.
This means you will not need to use: $name = $_POST['name']; anymore. As the above code will set the name variable for you and also set the session variable.
The best explanation of passing variables either with form post or header location redirection inside php check out my answer in this thread: How to pass variables received in GET string through a php header redirect?

PHP 4 Variable losing value on scope change

echo $uid; // RETURNS INTEGER VALUE
if (isset ( $_POST ['cashpaid'] )) {
$queryfinal = "select * from " . $db_prefix . "customer_det
where `id` = '$uid'"; // UID RETURNS NULL
....
I've tried everything and every combination of globals and super globals that I could think of. There's just no way I can transfer the value over.
I pull $uid from a MySQL select and it assigns to an integer value. As you can see at the start of the code, that ending curly brace is the end of an IF statement which contains the value for $uid.
How can I assign the same value to $uid across all scopes?
This has been killing me for about two days. I'm sorry if this seems elementary but it's about to drive me nuts. I tried $GLOBALS['uid'] to no avail.
You could try with a setter/getter function declared once in the global scope, but it doesn't do much more than accessing GLOBALS anyway.
function globalUid($value = False)
{
GLOBAL $__uid;
if (!isset($__uid))
$__uid = False;
if (False === $value)
return $__uid;
$__uid = $value;
}
Then instead of echoing $uid, try globalUid($uid) and, later, $saved_uid = globalUid();.
But I think it's possible the two scopes are executing across two different calls, so you might need to save the UID in the session variables.

store mysql results in SESSION array for later use (to check via in_array)

I currently have this :
require_once('auth.php');
auth.php:
session_start();
if(!isset($_SESSION['SESS_MERCHANT_ID']) || (trim($_SESSION['SESS_MERCHANT_ID']) == '')) {
header("location: login-form.php");
exit();
}
mybadges.php:
$mybadges = mysql_query("SELECT badge_id
FROM badges WHERE merchant_id = $current_userid ORDER BY badge_id DESC");
while ($result = mysql_fetch_array($mybadges)){
$badge_id = $result['badge_id'];
}
I wanted to know how I can store $result['badge_id']; in a $_SESSION array (like $_SESSION['badges']?)
a more sensible version of 'auth.php'
session_start();
if(empty($_SESSION['SESS_MERCHANT_ID'])) {
header("location: login-form.php");
exit();
}
a more sensible version of mybadges.php:
$sql = "SELECT badge_id FROM badges WHERE merchant_id = $current_userid ORDER BY badge_id DESC"
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
in case there is only one bagde_id to store:
$row = mysql_fetch_array($res);
$_SESSION['badge'] = $row['badge_id'];
in case there are many id's (as one cannot say for sure from your code, what you need):
$badges = array();
while ($row = mysql_fetch_array($res)) {
$badge_ids[] = $row['badge_id'];
}
$_SESSION['badges'] = $badge_ids;
in general, to store anything an a session, just assign that anything to a session variable:
$_SESSION['badges'] = $badge_ids;
not a big deal.
a SESSION array become exectly what you wrote. $_SESSION['badges'] is a regular variable to use.
Note that you can save only scalars, arrays and objects. Not resources.
If I understand well your needs, you can simply do this :
$_SESSION['badges'] = $result['badge_id'];
Make sure that mybadges.php includes auth.php otherwise you won't have a session started. I'm assuming that's what you have but it doesn't show up that way in the code above. Assuming you have auth.php included, just do as sputnick said with
$_SESSION['badges'] = $result['badge_id'];
php sessions support multiple dimesion arrays.
$members=array();
foreach(mysql_fetch_array($memberresultset) as $key->$value)
$members[]=$value;
foreach($members as $mv)
$_SESSION["MEMBER"][]=$mv;
would work, for example.
Also something like this:
$_SESSION["badgeidnumbers"][]=$result["badgeid"]
But your would need to add them with foreach command, fetching more than one rows.
Extra brackets are for adding to the array using the new index number. you can write anything in the second brackets.
you also use some format like
$_SESSION["badgeidnumbers"][$result["badgeid"]]=$result["badgename"]
I do not recommend dividing your project into too many files. Normally, you shouldn't need session variables. Don't hesitate to ask ideas about the general structure of your application. It may save you a lot of time.

PHP/Session/IE: Constant are saved, variable not

I have a very strange problem.
Situation: Session handling over MySQL, PHP 5.2.4, wildcard cookies, FF/Opera/Safari/Chrome works, IE7/8 not.
When I save a variable into the session, the value is lost. The DB shows after the write action only "N" instead of "123456".
Example:
$bar = 123456;
$_SESSION['foo'] = $bar;
But when I save a constant in the session, it works.
$_SESSION['foo'] = 123456;
This is not really a client problem, but only in IE it doesn't work.
Any ideas?
Edit:
This is the session write function:
function _write($id, $data) {
$write = "UPDATE session SET time='".time()."', data='".mysql_real_escape_string($data)."' WHERE id='".mysql_real_escape_string($id)."'";
$result = #mysql_query($write);
if(mysql_affected_rows()) return $result;
else {
$write = "INSERT INTO session (id, time, data) VALUES ('".mysql_real_escape_string($id)."', '".time()."', '".mysql_real_escape_string($data)."')";
return #mysql_query($write);
}
}
When I print the update query ($write) everything looks fine. I can execute the SQL manually and it works, also with variables.
Maybe sessionId in cookie every time is refreshing in IE?
SO every time - new session

Categories