How can I destroy a session in my database table. PHP coding will show at below :
<?php
session_start();
$session_id = session_id();
error_reporting(0);
include("db.php");
include("header.php");
if(isset($_POST['submit']))
{
$session_id=$session_id;
$actual_price=$_POST['actual_price'];
$gst=$_POST['gst'];
$granttotal=$_POST['granttotal'];
$product_id=$_POST['product_id'];
$quantity=$_POST['quantity'];
/*$product_name=$_POST['product_name'];
$product_price=$_POST['product_price']; */
foreach($product_id as $key => $pro){
$qty = filter_var($quantity, FILTER_SANITIZE_NUMBER_INT);
$pro = filter_var($pro, FILTER_SANITIZE_STRING);
/*$proname = filter_var($product_name, FILTER_SANITIZE_STRING);
$proprice = filter_var($product_price, FILTER_SANITIZE_NUMBER_INT);*/
$sql=mysql_query("INSERT INTO `checkout`(`session_id`,`actual_price`,`gst`,`granttotal`,`product_id`,`quantity`,`joinon`)VALUES ('$session_id','$actual_price','$gst','$granttotal','$pro','$qty','".date('Y-m-d')."')") or die(mysql_error());
mysql_query("delete from cart where sessionid='$session_id'");
echo ("<script>location.href='billing_info.php'</script>");
}
}
Now for my purpose I deleted a session using delete query But session deleted but when again add a product it shows a same session. Then I realise we should destroy a session so please help me.
Seems you are not working Session properly.
First store the variables in session such as :
$product_id=$_SESSION['product_id'];
$quantity=$_SESSION['quantity'];
If you want to remove the session, use the destroy session function,
session_destroy();
and unset your variables from session like this:
unset($product_id);
unset($quantity);
If I am getting it right then you want to destroy the current session so that you can reinitialize the cart according to the session id.
For destroying session in php you can use the below function after you perform the delete query successfully.
session_destroy();
For more info related to how to delete session_destroy() , you may find here the official documentation of php related to it.
Using this function this will destroy all the session variables ie; it will unset all the session variables that is stored inside the $_SESSION global variable.
But if you want to delete only specific session variable then you can use the following code:
unset($_SESSOIN['variable_name'])
For more info related to how to unset session variable
Edit : If you want to regenerate the session id then use the session_regenerate_id() function,
this is because you are using php variables session_id() which changes its value on the clearing session from browser or you open the page in different session. if you are deleting it and inserting it again with closing the browser it won't change its value.
Instead you can use this session_regenerate_id() which regenerate its value when you create it again. Here is the Manualenter link description here
After successful delete operation do this below steps
// Unset all of the session variables.
$_SESSION = array();
// Finally, destroy the session.
session_destroy();
If it does not work then let me know your PHP version
Related
<?php
if(isset($_GET['Ref'])){
include"secure_db.php";
$LangName=mysqli_real_escape_string($conn_server_link,$_GET['Ref']);
//Getting name..
$LangQuery=mysqli_query($conn_server_link, "SELECT * FROM
`language_store` WHERE `language_store`.`Lang_Id` = $LangName");
$FetcHName=mysqli_fetch_array($LangQuery);
$get_Lang_Id=$FetcHName['Lang_Id'];
$get_Lang_Name=$FetcHName['Lang_Name'];
session_start();
$_SESSION['lang'] = "$get_Lang_Id";
header('Location: home/');
}
?>
I have this code. I received a id using php $_GET method and I started a session using GET value. Now I want to update the session value and destroy the current value. anybody please suggest me what would be the process.
You can use the function session_regenerate_id() to replace the ID for the session:
http://php.net/manual/en/function.session-regenerate-id.php
Have a read through the manual page, including all of the comments on that page and the warning that it has about the usage of the function
I'm a noob programmer so I apologies in advance for any obvious mistakes. I've spent the past week creating a product database kinda thing. I've got too the point where I can add products using a form, view all products added etc. I've being using sessions which are created via the form input data. I'm struggling to include get a delete product page working, I've tried using unset to clear the variable but can't get it too work.
ADD Product page which sets the session variable:
$_SESSION['Products'][] = $_POST; //is how i set the session on the add products page.
unset $_SESSION['Products'][]; //is how i have tried to clear the session although it does not work.
Any point in the right direction will be appreciated!
You can unset session variable using:
session_unset - Frees all session variables (It is equal to using: $_SESSION = array(); for older deprecated code)
unset($_SESSION['Products']); - Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)
session_destroy — Destroys all data registered to a session
To know the difference between using session_unset and session_destroy, read this SO answer. That helps.
I am including this answer in case someone comes to this page for the same reason I did. I just wasted an embarrassing amount of time trying to track the problem down. I was calling:
unset($_SESSION['myVar']);
from a logout script. Then navigating to a page that required login, and the server still thought I was logged in. The problem was that the logout script was not calling:
session_start();
Unsetting a session var DOES NOT WORK unless you start the session first.
Unset is a function. Therefore you have to submit which variable has to be destroyed.
unset($var);
In your case
unset ($_SESSION["products"]);
If you need to reset whole session variable just call
session_destroy ();
If you completely want to clear the session you can use this:
session_unset();
session_destroy();
Actually both are not neccessary but it does not hurt.
If you want to clear only a specific part I think you need this:
unset($_SESSION['Products']);
//or
$_SESSION['Products'] = "";
depending on what you need.
unset is a function, not an operator. Use it like unset($_SESSION['key']); to unset that session key. You can, however, use session_destroy(); as well. (Make sure to start the session with session_start(); as well)
Destroying a PHP Session
A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.
Here is the example to unset a single variable
<?php unset($_SESSION['counter']); ?>
Here is the call which will destroy all the session variables
<?php session_destroy(); ?>
// set
$_SESSION['test'] = 1;
// destroy
unset($_SESSION['test']);
$_SESSION['Poducts'] = 1; // set
unset($_SESSION['Products']); //unset
All the answer about unset are correct but one thing is needed to be corrected. If you did not use session_start() the unset() will never work. I recommend doing it this way
session_start();
unset($_SESSION['productID']);
I started a session $_SESSION['ProdID'] = $ProdID; earlier in my code and I started another ProdID session in another page of my script.
I want to end the first one while this new one will be active without logging out.
Create a page with any name you want.
For example you create a page named as logout.php and paste this code in it.
<?php
session_start();
session_destroy();
header('location:login_page.php');
?>
if you want to destroy all sessions , it's better to use session_destroy()
if you want to destroy specific session , you can use unset($_SESSION['']);
First destroy the current session by regenerating a new session ID to create new cookies. You can then set your values in the new session, the old session is destroyed. Optionally delete all old session variables if you don't need them any longer:
/* generate new session id and delete old session in store */
session_regenerate_id(true);
/* optional: unset old session variables */
$_SESSION = array();
/* set new value(s) */
$_SESSION['name'] = 'value';
If you still want to keep the old session ("without logging out") you can remove the true parameter so the old session is kept in store:
/* generate new session id and keep old session in store */
session_regenerate_id();
The rest would remain the same.
Try using session_destroy(); to end your current session.
Use
unset($_SESSION["ProdID"]);
Only type unset session end of code,
Like this
unset($_SESSION['ProdID']);
When I log a user out of an app I am building I use session_destroy();
But when I go back to the page, all session variables are still set.
How can I completely destroy all session variables and ultimately require a user to log back in again?
Here is my code:
session_unset(); // clears all session variables
$_SESSION = array();
session_destroy(); // deletes session id
Thanks
After using session_destroy(), the session cookie is removed and the session is no longer stored on the server. The values in $_SESSION may still be available, but they will not be on the next page load.
If you need to clear the values of $_SESSION, set the array equal to an empty array:
Of course, you can't access the values of $_SESSION on another page once you call session_destroy, so it doesn't matter that much.Still if you are concerned .
Try the following:
session_destroy();
$_SESSION = array(); // Clears the $_SESSION variable
you are not calling session_destroy() for sure, your code may be unable to access it.
Post more code so we could help you
I have this query in mysql in a php page:
mysql_query("INSERT INTO tz_todo SET text='".$text."',
position = ".$position.",
user_id=".$_SESSION['user_id'].",
view_stat=0");
I tried to echo the query and the result is this:
INSERT INTO tz_todo SET text='trial text', position = 21, user_id=, view_stat=0
it seems that it can't get the session value of user_id.
And $_SESSION['user_id'] is not working in social engine. How to correct this? I also made a localhost version in my xampp and everything is fine but when I converted it into social engine, session is not working.
In any page where you are using session objects, place this code at the beginning of the file:
if(!isset($_SESSION)){session_start();}
This way if the session is not already started, it starts it; otherwise it ignores the session start if the sesion is already started.
This is important because calling session_start() if session is started already can sometimes cause errors.
That's how I get my user id through session
session_start();
$userID = $viewer->getIdentity();
$_SESSION['user_id'] = $userID;
echo $_SESSION['user_id'];
Using session to store the user_id is totally wrong. To gain a user_id try
$viewer_id = Engine_Api::_()->user()->getViewer()->getIdentity(); (or $user->getIdentity if you have another user's object).
If you still need to use session for storing this data, use Zend-approach.
session_start();
$_SESSION["test"] = "hello world";
session_start();
echo $_SESSION["test"];
does above code work ? if not, check your session.save_path in the php.ini
NOTE: to retain this variable remember to call session_start() on each php script/page before calling for the variable from the session.
Yoy might be forget to start your session at the top of the page
<?php if(!isset($_SESSION)){ session_start(); } ?>
$_SESSION['user_id'] might not stored a value. check your login page (Basically after login session variables will set) or after register weather you assigned a value to that session variable..
setting a value to a session variable :
$_SESSION['user_id'] = "1234567";