retaining session variable in php - 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.

Related

use a php variable in diffrent pages

I have two pages of php on the same folder , Im wondering what can I do to use that variable in another page like page2.php
page1.php
$variable = "Variable";
page2.php
//get $variable value from page1.php here
Any suggestions? Thank You
Use Sessions.
On page1.php:
session_start();
$_SESSION['var1'] = 'some text';
On Page2.php:
if(!isset($_SESSION)){
session_start();
}
echo $_SESSION['var1'];
You will get some test as output.
Use session variables.
Just write session_start() at the beginning of files to start a session and assign the session variables.
eg:
File-1
session_start();
$_SESSION['var'] = "var";
File-2
session_start();
if(isset($_SESSION['var']))
echo $_SESSION['var']; // will print "var"
You can use this throughout till the session destroyed. To destroy the session (may be on logout)-
if(isset($_SESSION['var']))
unset($_SESSION['var']);
To completely destroy the session-
session_destroy();
Or you can try constants like so:
constants.php
<?php
define("VARIABLE_1", "Hotdog");
?>
page2.php
<?php
include_once("constants.php");
echo VARIABLE_1;
// output == Hotdog
?>
Just include the constants php script in the pages you want to use the variables in.
Check PHP Sessions:
page1.php
session_start();
$_SESSION['variable'] = "Variable";
page2.php
session_start();
echo $_SESSION['variable']
You can require_once("page2.php") in page1.php
That will allow you to use variables in page 2 across page 1.
Alternatively, you can also use sessions.
session_start() starts a new session or resumes an existing session. Include this on the first line of both pages.
Session parameters are set using $_SESSION["parameter-name"] = "something";
You can use the variables anywhere in the script.
To terminate sessions, use session_destroy()
You can use Sessions to achieve this , also you should know about QueryStrings.
For example:
Navigate to another page say "location2.php?Variable=123" here ?Variable is your var and 123 is value.
And on location2.php you can get this by simply $_GET["Variable"];
This can be achieved in many ways. You can use php session, you can include a file where you create that variable, or you can define it as a consant.
All of that depends how complex is that problem and which suits you best in this case.

multiple sessions on one web application

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

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 }?>

In php, how to destroy the session variables

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.

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