I'm not a PHP developer here.
I have a page that is unable to display session values even though they definitely exist. I am able to view them on another page, yet for some reason they cannot be seen on a certain page!?
EDIT:
Below is the script that exists on the top of the page
<?php
require_once('eu_gl.php'); // <- includes session_start() in it
if(!session_id()) session_start(); // added this in case, but should not be needed
?>
Contents of the include:
<?php
/*** Global include file **/
set_time_limit(300);
$time1 = microtime();
define('APP_SESS_NAME', 'EURA');
session_name(APP_SESS_NAME);
session_start();
session_set_cookie_params(0);
//...
?>
As #k102 mentioned, ensure you have session_start(); somewhere before you set/get your session variables. print_r($_SESSION); can also be handy in showing you what session information exists ...
I personally would modify your code to have this:
if (!isset($_SESSION)) session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
print_r($_SESSION);
First point is :
better try
if(session_id() == "" ) session_start();
instead of
if(!session_id()) session_start();
as session_id wont return false.
php manual:
session_id() returns the session id for the current session or the
empty string ("") if there is no current session (no current session
id exists).
Second point is that session_start shall be the very first thing on your page unless you really need sthg else, but I can't see any reason in your code.
You have to make 2 php file for checking $_SESSION, session used for storing variable so you can use it in all page of your website.
test.php:
set_time_limit(300); // Timeout for script
$time1 = microtime(); // What this variable do in your script
define('APP_SESS_NAME', 'EURA'); // set constant APP_SESS_NAME
session_name(APP_SESS_NAME); // name session APP_SESS_NAME
session_start(); // start session
session_set_cookie_params(0); // this line must be called before session_start();
if(!session_id()) session_start(); // Delete this line
$_SESSION['name']= "test"; // set variable session with params name for checking session
test2.php:
define('APP_SESS_NAME', 'EURA'); // set constant APP_SESS_NAME
session_name(APP_SESS_NAME); // name session APP_SESS_NAME
session_start(); // start session
echo $_SESSION['name']; // check session is valid
I think you should understand about Session now.
Seems the naming of the session was the issue. Tt was named in the include: session_name(APP_SESS_NAME), then it seems I have to use that session name when starting it elsewhere.
Related
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?
I know I can use $oldSession = session_name("mySessName"); to set the name of the session, which I do like so:
# FileName: sessionTest.php
$old_name = session_name("TEST");
session_start();
$_SESSION["hi"]="hi";
print_r($_SESSION);
I can even have another file: sessionTest1.php which contains the following:
# FileName: sessionTest.php
$old_name = session_name("TEST1");
session_start();
$_SESSION["Bar"]="bar";
print_r($_SESSION);
I can go back and forth between sessionTest.php and sessionTest1.php and the session will only have the corresponding variable.
The issue I am running into is suppose a different script already has a session started and then calls this file. What I am seeing is suppose I have:
session_name("other");
session_start();
$_SESSION["foo"] = "foo";
require_once "sessionTest.php";
print_r($_SESSION);
This is printing Array( "foo" => "foo", "hi" => "hi" ). Is there a way to end the previous session and start my session fresh. Note: I don't want to destroy the previous session as there may be valuable information in it.
what i do is make my SESSION 1 layer deeper then the standard. so i can just use that layer of the array.
some page:
<?php
$_SESSION['myApp1']['hi'] = "Hi";
?>
some other page:
<?php
$_SESSION['myApp2']['ciao'] = "Ciao";
?>
so when i want to see session vars on page 2 i just
<?php
echo "<pre>";
print_r($_SESSION['myApp2']);
echo "</pre>";
?>
use session_name before session_start.
PHP session_name
The session name is reset to the default value stored in session.name at request startup time. Thus, you need to call session_name() for every request (and before session_start() or session_register() are called).
read this SO answer:
Multiple Sessions
I am using PHP sessions for a tool I have created. It allows for you to resume a previous session you may have started that is stored in the database. All that functionality is working as intended.
However, I provide a link that says "Create New Session" and point it to a PHP page that contains this code:
<?php
session_start();
session_destroy();
$_SESSION = array();
unset($_SESSION);
header('Location: wizard.php');
?>
Now, when it redirects back to wizard.php, I have it printing out all session details and it still contains information from the previous session.
Is there something I am missing here?
Wizard.php starts with session_create(); so I would assume as soon as it redirected it would create a new session ID and all which isnt happening.
Thanks for any info
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// Finally, destroy the session.
session_destroy();
header('Location: wizard.php');
?>
Taken from: session_destroy Example 1
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 }?>
my problem is that the session variable did not get unset when running the below code.
what is wrong?
<?php
session_start();
session_unset();
//session_destroy();
header("location: user_form.php");
?>
You've not actually created a session you've started the session engine but not created a session variable.
If you have a session variable $_SESSION['userid'] for example then you can just unset that value or expire it or set its value to something that would fail your if clause for your header redirect.
Usually I do something like:
<?php
session_start();
if(!empty($_SESSION) && is_array($_SESSION)) {
foreach($_SESSION as $sessionKey => $sessionValue)
session_unset($_SESSION[$sessionKey]);
}
session_destroy();
header("Location: user_form.php");
?>
Try this syntax (use a variable name in unset):
<?php
session_start();
if(isset($_SESSION['views']))
unset($_SESSION['views']);
?>
I'm guessing you already have variables within your session set, otherwise there would be nothing to "unset".
With session_unset the session itself would still exist, as it's just the equivalent of doing:
$_SESSION = array();
Unless of course you're using PHP 4.0.6 or below, then you would be expected to use:
unset ($_SESSION['varname']);
as per session_unset.
There isn't anything "wrong" with your code so to speak.