Getting data from Php session - php

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.

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.

how to start a session by adding a link and end the session in the same way and showing session datas

I have to add a link in my search screen to start a session by the user and and another link to stop the session in the result page. The result page will also will have a link to show all the wine names. I know only the basic session(). I am not getting what I have to do or code should i follow. Please suggest me something, if possible example codes.
Here is how you can end the session with a link, by passing a $_GET parameter
Log out
<?php
if(isset($_GET['logout'])) {
session_destroy();
}
?>
It is worthy to note, that you must have already started the session with session_start() before destroying it.
Create Session
Show Sessions
<?php
//must have session start before destroying or starting sessions
session_start();
if(isset($_GET['create']))
{
//setting sessions with time, this can be equal to anything string
$_SESSION[] = time();
}
else if(isset($_GET['show']))
{
//this display all sessions currently stored
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
}
?>
You need to initialize the session if you want to destroy. So use this it should work
<?php
if(isset($_GET['start'])){
session_start();
$_SESSION['key']=true;
}elseif(isset($_GET['stop'])){
session_start(); // this is need to destroy also
session_destroy();
}
$ses_id = session_id();
if(empty($ses_id)){ ?>
Start Session
<?php }else{ ?>
Stop Session
<?php }?>

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.

php sessions not working correctly

Hello i am having problems holding sessions from page to page, code worked on my previous servers running php5 but not on my recent server, i am wondering whether its a bug?
<?php
session_start();
$_SESSION['session'] = $_POST['session'];
header("location: www.mysite.com/page1.php");
?>
<?php
session_start();
echo "Good morning" . $_SESSION['session']; //returns empty session always.
?>
ANy ideas? session is held on first page but not on the second.
In case you missed it, make sure you do a session_start() at every page you're using the $_SESSION variable.
You should check your php.ini file and see what's going on.
Make sure session.use_cookies = 1 and session.save_handler = files.
Use this test page to see whether it's a general PHP problem or just your code.
<?php
session_start();
if(isset($_SESSION)){
echo "Session variable exists<br/>";
if(!isset($_SESSION['test'])){
$_SESSION['test'] = "Success!";
echo "Variable has been set, refresh the page and see if stored it properly.";
}else{
echo $_SESSION['test'];
}
}else{
echo "No session variable has been created.";
}
?>
If that worked, then it's got to do with your code.
If you're setting your session variable to $_POST['session'] am I to assume you submitted a form with an input with the name session?
This setup should work.
index.php
<form action='page0.php' method='POST'>
<input type='hidden' name='session' value='SPAAAAACE' />
<input type='submit' />
</form>
Page0.php
<?php
session_start();
$_SESSION['session'] = $_POST['session'];
header("location: www.mysite.com/page1.php");
?>
Page1.php
<?php
session_start();
echo "Good morning" . $_SESSION['session'];
?>
For completeness and debugging purposes
In case you are using cookie-less sessions, you have to manually add the SID (session id) to the header redirect like this
header("location: www.mysite.com/page.php?".htmlspecialchars(SID));
If the problem still persists, it could be a permission issue.
Maybe you're not allowed to read the session file stored on the server?
Update: OP commented that it was a permission issue and the problem is now resolved
Turn on error reporting temperately with:
error_reporting(E_ALL) This may spit out an error related to your problem. Most likely an undefined index session notice.
You should always have a check in place on Super Globals.
<?php
session_start();
$_SESSION['session'] = (isset($_POST['session']))?$_POST['session']:null;
header("Location: www.mysite.com/page1.php");
die;
?>
Your code seems correct though I'm pretty sure $_POST['session'] is empty.
You should try this :
<?php
session_start();
$_SESSION['session'] = 'John Doe';
header("location: www.mysite.com/page1.php");
?>
<?php
session_start();
echo "Good morning" . $_SESSION['session']; //returns empty session always.
?>
To see if this works or not. I guess it will.
IF not, take a look at your cookies, maybe they are disabled.
Then, if it works, I probably because $_POST['session'] is null or empty, are you sure you posted something like <input type="text" name="session" /> ?
You need to pass the session id with the redirect.
Also make sure you use session_start() at the top of EVERY page that needs a session
First try using
<?php session_start();
instead of
<?php
session_start();
If the problem still exists, then open your script in Netbeans editor and see whether any unexpected characters found at very beginning of the the script.
In addition, please make sure that $_POST['session'] has a value to assign in $_SESSION['session'].
You will have to call
session_start();
on the first line of every page you want to retain the session in

retaining session variable in php

i want to retain the session variable while browsing from one page to the other
could some one help me that i could i achieve that??
thanks
Ok in the most basic fashion
<?php
//index.php
session_start();
$_SESSION['name'] = "Fred";
header("Location:displayname.php");
?>
<?php
//displayname.php
session_start();
echo $_SESSION['name'];
?>
Its made for it. I think you are missing the session_start() part of your script.
Then you can register a var as following: $_SESSION['var_name'] = 'value'; and you are done, if the session doesn't expires, that variable will be available everywhere on your domain.
Remember that session, by default, expires after 24 minutes of inactivity.
Basically the code should look like this:
session_start();
$_SESSION['var'] = 'var';
and you should be able to get the variable in another page:
$var = $_SESSION['var'];
Here it is. Just don't forget the session_start() at the top of each page where you need to use sessions.
This way,
<?php
session_start();
$_SESSION['views'] = 1;
echo "Pageviews = ". $_SESSION['views'];
?>
The data in _SESSION will be persisted across multiple pages until you invoke session_destroy() function.

Categories