Session data not saved on disk and also when processing another page - php

I am trying to process simple HTML form when on submit page1.php is called. page1.php internally calls page2.php
page1.php looks like this:
<?php
session_start();
$sesid = session_id();
$_SESSION['cname'] = #trim(stripslashes($_POST['companyname']));
$_SESSION['fname'] = #trim(stripslashes($_POST['firstname']));
$_SESSION['lname'] = #trim(stripslashes($_POST['lastname']));
$_SESSION['eaddr'] = #trim(stripslashes($_POST['eaddress']));
echo " $_SESSION[cname]" ;
echo " $_SESSION[otprdsvc]";
include_once $_SERVER['DOCUMENT_ROOT'] .'/appconnector.php';
?>
Page2.php looks like this:
<?php
echo "$_SESSION[cname]";
?>
I do get output of echo $_SESSION[cname]; on first page. However, data is not saved to disk when I check session cookie.
Here is data from session cookie on disk:
cname|s:0:"";fname|s:0:"";lname|s:0:"";eaddr|s:0:"";webaddr|N;cmsg|s:0:"";drpopt|N;otprdsvc|s:0:"";securimage_code_disp|a:1:{s:7:"default";s:6:"n55Zmr";}securimage_code_value|a:1:{s:7:"default";s:6:"n55zmr";}securimage_code_ctime|a:1:{s:7:"default";i:1398221627;}
I don't understand why cname is coming as "" ( no data / null ) where in fact I am getting response.
Little bit of history what I have done till now
session.save_path was not enabled earlier in php.ini file so I enabled it.
I was using autocomplete = off in html form earlier so I removed it.
securimage is packaged solution I am using for captcha which is saving cookie data but I don't know+understand anything what it did internally.
Can you please suggest what needs to be done in order to have session cookie data:
saved & available on disk in file
make it available when page2 is processed.
Thank you in advance and I apprecriate your time and comments.

You will have to always do session_start(); on every page before using the $_SESSION variable.
So page2 will be
<?php
session_start();
echo "$_SESSION[cname]" ;
?>

Try using this:
For page1.php
....
echo $_SESSION['cname'];
echo $_SESSION['otprdsvc'];
....
For page2.php
<?php
session_start();
echo $_SESSION['cname'];
?>
You have to use session_start() on every page wherever you want to call the session data.

I have solved this problem. Page2.php was submitting itself so session data was getting lost. I now have modified flow of data processing. I also used session.write_close() after writing data to session variables on page1.php so that also helped.
Thank you for your comments.

Related

PHP Sessions Variables & Authentication

I have two pages which I need to pass session information between them. Here's page one.
<?php
session_start();
echo session_id()."<br>";
echo $_SESSION['test']."<br>";
Page two.
<?php
session_start();
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];
Page two is in a different directory (same domain), and has Windows Authentication on. Page one is using anonymous authentication. From the output I can see the session ID is the same, but page one doesn't echo the test variable set from page two.
I'm running PHP in IIS 8.5 on Server 2012 R2.
Any help is appreciated.
To clarify, I am calling page two first, and page one will not show the variable.
You need to initialize the variable with a value first before using it.
First run page2.php so, that value of $_SESSION['test'] is set to test
using $_SESSION['test'] = 'test';
Now, run page1.php there you can see the value of $_SESSION['test']
using echo $_SESSION['test']."<br>";
you can also follow these steps to make use of session in a simple way so that you don't have to do session_start() again and again
1. config.php having session_start();
2. page1 having include_once('config.php');
echo session_id()."<br>";
echo $_SESSION['test']."<br>";
3. page2.php having include_once('config.php');
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];
Try closing your session in the first page, it's possible that your session file is still open when the second page is called, so you're not seeing the data. Try adding session_write_close() to the first page called.
http://php.net/manual/en/function.session-write-close.php
The issue was with permissions on the session save path. I changed the path to where both directories could write and it works.

Fetching value stored in session on previous page with another session name using php

Here is my scenario.
I have 2 different pages with php
1). index.php page have session name declared as "session_one"
$some_session = session_name("session_one");
session_set_cookie_params(0, '/', '.domain.net');
session_start();
2). order.php page have session name declared as "sesson_random" (this is required to have another session name due to nature of implementation
$some_session = session_name("sesson_random");
session_set_cookie_params(0, '/', '.domain.net');
session_start();
now the issue that I am facing is that I have stored some values on index.php in session which I want to retrieve on order.php. I have tried many ways but unable to pass it.
Please note that I can not pass those values in query string of url.
Please help
You should be able to read the data from the other session, by restoring it before you open the new one. All you have to do is use the session_id function. I tested it with this code right here: (index.php)
<?php
session_name("session_one");
session_start();
$_SESSION["test"] = array("this is just a test");
print_r($_SESSION);
?>
Now, all you have to do is load the other session first and save the values into an array: (order.php)
<?php
if(isset($_COOKIE["session_one"])){
session_id($_COOKIE["session_one"]);
session_name("session_one");
}else{
session_name("session_one");
}
session_start();
$session = $_SESSION;
session_write_close();
print_r($session);
if(isset($_COOKIE["session_random"])){
session_id($_COOKIE["session_random"]);
session_name("session_random");
}else{
session_name("session_random");
}
session_start();
$_SESSION["other"] = array("this is another test");
print_r($_SESSION);
?>
The two sessions get combined. If you are not bothered by that, you should be good to go. Got some inspiration from here:
Can multiple independent $_SESSIONs be used in a single PHP script?

How to set unset session variable back?

I'm trying to test my php script, so I need to (at least temporarily) get back some of the unset variables.
I mean:
page1.php:
<?php
session_start();
$_SESSION['var']="somevalue";
go_to("./page2.php");
?>
page2.php:
<?php
session_start();
use($_SESSION['var']);
unset($_SESSION['var']);
?>
Now when I go to page1->page2, then page1 again (then page2 again) page2 does not recognize $_SESSION['var'], so I need something like
if(!isset($_SESSION['var']))
{
set_back($_SESSION['var']); //But how ?
}
in page1.php.
Any help would be appreciated. Thanks!
I think that your problem is the local browser cache.
When you go back to page1 the response is served from your computer cache instead from the server so the session variable is not set again
There is no functionality in php that "rolls back" a session/variable that has been unset (Of course you could store the original value in temporarily session like Boaz suggested). What exactly are you trying to achieve?

session is always empty outside of the file where is generated

Why I am getting an empty array of the session when I am trying a var_dump in another page?
In the server, the session is stored without any content, only with the name of the id.
With cookies all works well. Sometimes (yes, sometimes), I restart the server, and then, the sessions works well too. What may cause this issue?
Maybe a bug of the php 5.1.6? or a problem in the config of the server?
index_2.php
<?php
session_start();
$_SESSION['xxx'] = "tessstsse";
var_dump($_SESSION);//here show the correct session
header( 'Location: index_3.php');
index_3.php
<?php
session_start();
var_dump($_SESSION);
The output of this will be:
array
empty
Give this a try.. if this works I'd re examine your code else it is probably an issue with your server...
page1.php
<?php
session_start();
$_SESSION['auth'] = "true";
$_SESSION['superhero'] = "batman";
?>
Click here
page2.php
<?php
session_start(); // start the session before using it
//echo $_SESSION['auth']; // will output 'true'
print_r($_SESSION);
?>
destroy.php
<?php
session_start(); // start the session before using it
session_destroy(); // deletes the current session..
?>
I have the web server without space to store the session content.
0% of free space. Delete some data and the problem is solved.

Getting data from Php session

I have created a php session and have assigned data like so
session_start();
$_SESSION['desd']=$imgD;
When i try to retrieve it on other page i get nothing.
this is what i have done on second page,
session_start();
$_imgname = $_SESSION['desd'];
How to do that?
The code is valid, assuming that the variable $imgD is defined (var_dump is one way of checking this, but print or echo may also work).
Check to ensure that cookies are enable in your browser.
Also, be sure that session_start() comes near the top of your script, it should be the first thing sent to the client each time.
To test a session, create "index.php" with the following code:
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;
echo "views = " . $_SESSION['views'];
?>
Reload the page several times and you should see an incrementing number.
An example of passing session variables between two pages is as follows:
PageOne.php
<?php
session_start();
// makes an array
$colors=array('red', 'yellow', 'blue');
// adds it to our session
$_SESSION['color']=$colors;
$_SESSION['size']='small';
$_SESSION['shape']='round';
print "Done";
?>
PageTwo.php
<?php
session_start();
print_r ($_SESSION);
echo "<p>";
//echo a single entry from the array
echo $_SESSION['color'][2];
?>
For simplicity put PageOne.php and PageTwo.php in the same directory. Call PageOne.php and then PageTwo.php.
Try also tutorials here, here, and here.
If you call session_start(), the server gets the session ID from cookies.
Maybe something is wrong with the cookies because the code is valid.

Categories