I want to store some information like user_name, country_name etc and need to be available through out the session.
what will be the best way to achieve this?
I have the following code to $_session variable but have no result.
page1.php
session_start;
$_SESSION['country_no']=1;
page2.php
session_start;
echo "here is session variable=".$_SESSION['country_no'];
exit;
session_start() is a function, use it like this:
session_start();
$_SESSION['country_no']=1;
You missed the ()
session_start();
session_start(); //needed before you create any session variables.
$_SESSION['mysessionvariablename'] = $myVar;
Read up on the session_start() function.
put session_start(); add the top of you page.
try session_start(); instead of session_start;
You're using a method :)
Related
In my website the index will redirect to a controller that will access the DAO and needs set the data in a variable to show in the view. How do i set this datas? Is the $_SESSION the best way to do that?
I try the $_REQUEST, making this:
(Index.php)
<?php
$_REQUEST['test'] = "TEST!!!!";
$redirect = "controllers/controllerIndex.php";
header("location:$redirect");
(controllerIndex.php)
<?php
echo $_REQUEST['test'];
But i received the error:
Notice: Undefined index: test in C:\xampp\htdocs\PlataformaPHP\controllers\controllerIndex.php on line 2
As mentioned already, PHP SESSION variables are the solution you're looking for. It is important that you include session_start(); at the top of each PHP page that requires access to the session variable. Take a look at this which should help with your problem:
Index.php
<?php
session_start();
$_SESSION['test'] = "TEST!!!!";
$redirect = "controllers/controllerIndex.php";
header("location:$redirect");
exit();
?>
Controller.php
<?php
session_start();
echo $_SESSION['test'];
...
?>
You answered your own question, $_SESSION would be better.
You can also unset all your $_SESSION variables as described here
http://php.net/manual/en/function.session-unset.php
Using $_SESSION is probably the best solution, but the other option would be to just add the parameters to your header() url and they will be passed in the $_GET array.
$redirect = "controllers/controllerIndex/test";
header("location:$redirect");
Or without the Framework idea
header("Location: xxx.php?test");
I am trying to get rid of a bunch of session variables in PHP. I mean completely get rid of them.
I have tried some different approaches. For example:
$_SESSION = array();
session_destroy();
header('location: '.MAINPATH);
I have also tried using various compinations of session_unset, unset, setcookie etc. with the above commands.
I have of course tested if the session variables remains by doing:
echo $_SESSION['member_id'];
All of my session variables still remains for som reason.
Can anyone figure out what the problem might be?
Any help is greatly appreciated.
FWI: I am using PHP 5.5
UPDATE:
I tried changing my code to the following:
echo $_SESSION['member_id'];
session_unset();
session_destroy();
echo $_SESSION['member_id'];
which resulted in this output:
1000004 Notice: Undefined index: member_id in...
This should mean that the session variable is deletede right? The weird thing is, that when I am going back to my front page, the session variable is available again.
One more time, can you do a quick try:
<?php
session_start();
$helper = array_keys($_SESSION);
foreach ($helper as $key){
unset($_SESSION[$key]);
}
?>
you can use this code :-
session_start();
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
use within php tag
You can use session_unset() to clear all session variables.
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.
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.
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.