user-defined function for unsetting session is not working - php

I want a simple function where give a session it just echo it and then unset it. I have written something i have written this:
function sess_caller($sess){
if(isset($sess)){
echo $sess;
}
unset($sess);
}
It does echo the session but doesn't unset it afterwards.

function killSession($key="")
{
if($key!="" && isset($_SESSION[$key]))
{
unset($_SESSION[$key]); //this will empty the key value pair from the
$_SESSION (global) variable in php.
}
else{
//if not needed session anymore just destroy it.
session_destroy();
}
}

Related

How to check if session array has an id which is unique when all retrieve value has same session key in laravel

Please i'm working on a quiz web app, i'm getting my data randomly from database and i'm saving it in a session array. now i want to check if the id of the retrieved data is in the session array if it is i want to generate a new value if is not in i want to push it to the array i have searched but not being able to figure it out any help will be appreciated thanks in advance.
This is what i did initially but the session exist is always returning true since i'm using same key;
function random()
{
$output=DB::table('questions')->inRandomOrder()->first();
return $output;
}
function checkRandomvalue($stored){
if(session()->exists('question'))
{
random();
}
session()->push('question',$stored);
}
do{
$r=random();
$result=checkRandomvalue($r);
}
while($result);
And this is what i did now checkRandomvalue function but this $question=session('question.0.id',[]); is returning null making the index to be always false
function checkRandomvalue($stored){
$id=$stored->id;
$question=session('question.0.id',[]);
$index=array_search($id,$question);
//dd($index);
if($index==true)
{
random();
}
$question[]=$id;
session()->put('question',$id);
}
I finally solved my problem should incase anybody encounters same problem, i declared 2 session in the constructor one to store array and the second to hold session value not in the array so i can query my database with it
public function __construct()
{
$this->middleware('auth');
$val=[12];
$rand=$val[0];
Session::put('keys',$val);
$value=session('keys');
Session::put('Key2',$rand);
$v_random=session('Key2');
}
then this is method for my logic
function random()
{
$output=DB::table('questions')->inRandomOrder()->first();
return $output;
}
function checkRandomvalue($stored){
global $value;
global $v_random;
$id=$stored->id;
$value=session('keys');
if(array_search($id,$value,true)==true) {
return;
}else if(array_search($id,$value,true)==false){
Session::push('keys',$id);
session()->save();
$value=session('keys');
Session::put('Key2',$id);
//$v_random=session('Key2');
//dd("good", $id, $value);
//dd(session($key));;
}
}
do{
$r=random();
$result=checkRandomvalue($r);
}
while($result);
$v_random=session('Key2');
$output=DB::table('questions')->where('id',$v_random)->get();

Does session_set_save_handler method will lose the session data?

How can I get the session data when use session_set_save_handler?
When I refreshed the page, all the session data lost if i use the session_set_save_handler. however,if i delete the session_set_save_handler, all is right!
There is the code
<?php
/*
Session open (called by session_start( ))
Session close (called at page end)
Session read (called after session_start( ) )
Session write (called when session data is to be written)
Session destroy (called by session_destroy( ) )
Session garbage collect (called randomly)
*/
function sess_open($sess_path, $sess_name) {
//print "Session opened.\n";
//print "Sess_path: $sess_path\n";
//print "Sess_name: $sess_name\n\n";
return true;
}
function sess_close( ) {
//print "Session closed.\n";
return true;
}
function sess_read($sess_id) {
//print "Session read.\n";
//print "Sess_ID: $sess_id\n";
return '';
}
function sess_write($sess_id, $data) {
//print "Session value written.\n";
//print "Sess_ID: $sess_id\n";
//print "Data: $data\n\n";
return true;
}
function sess_destroy($sess_id) {
//print "Session destroy called.\n";
return true;
}
function sess_gc($sess_maxlifetime) {
//print "Session garbage collection called.\n";
//print "Sess_maxlifetime: $sess_maxlifetime\n";
return true;
}
session_set_save_handler('sess_open','sess_close','sess_read','sess_write','sess_destroy','sess_gc');
session_register_shutdown('session_write_close');
session_start();
if(isset($_SESSION['foo']))
$_SESSION['foo'] = "have the foo data";
else $_SESSION['foo'] = "no foo data";
echo $_SESSION['foo'];
?>
It always output 'no foo data',when I refresh the page.
if I delete the
session_set_save_handler('sess_open','sess_close','sess_read','sess_write','sess_destroy','sess_gc');
session_register_shutdown('session_write_close');
it always output 'have the foo data'.
Who can tell me why? Very thanks!

Cookie lost after another post submit

I have filters using $_POST and trying to store them into cookies.
It saves and remembers, but after another $_POST submit cookie is lost.
And yes I have in header.php file session_start() function.
Code:
setcookie('pP', $_POST['perPage'], time()+3600);
if(isset($_COOKIE["pP"]) && $_COOKIE["pP"]==25) {
$per_page=25;
} elseif(isset($_COOKIE["pP"]) && $_COOKIE["pP"]==50) {
$per_page=50;
} elseif(!isset($_COOKIE["pP"])) {
$per_page=25;
}
I've tried using $_SESSION variables too, but still nothing.
Code:
$_SESSION['pP']=$_POST['pocetZaznamu'];
if(isset($_SESSION['pP']) && $_SESSION['pP']==25) {
$per_page=25;
} elseif(isset($_SESSION['pP']) && $_SESSION['pP']==50) {
$per_page=50;
} elseif(!isset($_SESSION['pP'])) {
$per_page=25;
}
How can I fix this problem? I want use $_POST not $_GET and remember $_POST values.

jquery & php + if session is dead an error is returned

I've noticed if the session is dead (on an Jquery AJAX PHP Request) the data returned is preceeded with an error message if the session is needed in the request.
How do other sites deal with this?
Similar code is shown below - eg: its using a SESSION variable in the code which doesn't exist as the session is dead.
public function internal($variable) {
if($_SESSION['data'] == $variable) {
echo TRUE;
}else{
echo FALSE;
}
}
Should I use isset to check if the variable exists?
Should I be coding for dead sessions?
thx
You need to add the isset also:
public function internal($variable) {
if(isset($_SESSION['data']) && $_SESSION['data'] == $variable) { //add here
echo TRUE;
}else{
echo FALSE;
}
}
Try this
public function internal($variable) {
if($_SESSION['data']!="" && $_SESSION['data'] == $variable) { //add here
echo TRUE;
}else{
echo FALSE;
}
}
You can also do like this,
public function internal($variable) {
if(!empty($_SESSION['data']) && $_SESSION['data'] == $variable) { // modify here
echo TRUE;
}else{
echo FALSE;
}
}

Check that no variables have been passed

I have one file index.php, I am trying to include
splash.php when no variable is passed
and
display.php when any variable is passed.
This is what i have so far but i want to make it universal for all variables instead of just "query".
if (!isset($_REQUEST['query']))
{
include("splash.php");
}
else {
include("display.php");
}
if (count($_REQUEST) == 0) {
include("splash.php");
} else {
include("display.php");
}
though you're better checking $_POST or $_GET (as appropriate) rather than the looser $_REQUEST

Categories