I'm using a session variable to keep track of the current site language, three values are possible, 1. EN, 2. RU, 3. ES.
the session variable is set initially in the config file:
$_SESSION['lang'] = 'RU';
but inside my db class I cannot access the variable. My basic understanding is that variables stored in the $_SESSION array are accessible throughout the site.
so what's the problem?
Be sure to call session_start(); before using a session variables.
Before instantiating the class, you need to open the session.
For example:
session_start('NAME_MY_SESSION')
Related
Newbie question, but I'm wondering if I'm missing something elementary here.
If I register a session variable in a page - isn't this variable supposed to be accessible from another page on the same site?
First, I register a variable in the file session_var_register.php:
<?php
$_SESSION["myusername"] = 'user';
if (isset($_SESSION['myusername'])) {
echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>
When I open this page, it writes:
Session var myusername is set to user
As expected.
Then I open another tab and another page, check_session_var.php:
<?php
if (isset($_SESSION['myusername'])) {
echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>
This page is blank.
Isn't the point of a session variable that it should be accessible in the browser session, until the session is programatically destroyed or the browser closed?
I'm using IE 8 and Firefox 24, btw. Identical results.
You forgot
session_start()
On top, before using
$_SESSION
PS: Remember to call session_start() in every page you want to use $_SESSION.
The PHP docs state that you must call session_start() to start or resume a PHP session. This must be done before you try to access or use session variables. Read more here.
session_start();
Your session variables will be available on different pages of the same site but on top of each of these pages you must have at least:
session_start();
It works but not in all cases. You must also use the same session name (essentially a cookie name that stores id of your session) on all pages. Moreover cookies (which are essential (mostly) for sessions to work) may be made visible only in specific directory. So if for example you share the same host with other guys that use sessions too you do not want to see their variables and vice versa so you may want to have sth like that:
1) session_name( 'my_session_id' );
2) session_set_cookie_params( 0, '/my_dir', $_SERVER['HTTP_HOST'], false, true );
3) session_start();
You may also want to see your session variables on other servers and in such case custom session handlers may be useful. Take a day or two to implement yourself - great way to understand how sessions work hence I recommend.
Method
session_start();
Description
session_start() creates a session or resumes the current one based on a session identifier >passed via a GET or POST request, or passed via a cookie.
Usage in your case (and in the most of cases):
Put it before the $_SESSION usage.
Reference: session_start()
First Of all start session on that page
session_start();
your page like this way
<?php
session_start();
if (isset($_SESSION['myusername'])) {
echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>
I have my index page which uses a script to load another page into a div
$('#TransportPlanning').click(function() {
mainpage = $('#FloatMain')
mainpage.load('create_diary.php')
});
The page loads ok into my div, but I want to share php variables from one page to another, I thought the newly loaded page would be able to reference the main index variables but this is not the case , I have tried global but still not working
Any help please ?
To Share Variable Between Two Different PHP Scripts, Make It Super Global :
Use
session_start();
// store session data
$_SESSION['key']=value;
In index.php And read it in crate_diary.php as :
session_start();
$key=$_SESSION['key'];
And Do That ($key) Variable Specific code in create_diary.php.
Using Session or Cookies Prevent Unethical Use of Your Sensitive Data. You Can Also Use Cookies Instead Of Session. But Dont forget to unset session after you've done with it. Specially When you are dealing with cookies because Session will get automatically destroyed when user closes browser but this isn't true with cookie.
Global variables are not shared across scripts.
Pass them as query arguments instead, like:
mainpage.load('create_diary.php?key=value');
The value will be available within your create_diary script in $_GET['key']
How can I switch to a previously saved session using Zend\Session\SessionManager? I know the session ID.
For example, this doesn't work:
$sm->start();
$sm->setId('abc');
$_SESSION will not contain the data of session 'abc'. Calling $sm->writeClose() after $sm->start() doesn't help either.
I can easily do this using standard PHP functions:
session_start();
session_write_close();
session_id('abc');
session_start();
//$_SESSION is populated with 'abc' data.
Zend uses session namespaces for that. If you give the session a name like this
$sess = new Zend_Session_Namespace('abc');
you can access the contents via $sess->var and reload the session in a different PHP file just by creating the new session with the same name again.
http://framework.zend.com/manual/1.12/de/zend.session.basic_usage.html
I've one variable called numbers which I'm use within a session to identify a user. The link should be like index.php?number=1234567890, where number is the session name to be set and 123456789 is a user id. Can you explain how to do it "Pass through URL and SET as a var"?
index.php?number=1234567890
GET[???]
You can get it from $_GET as follows in your index.php
$number=$_GET['number'];
The variable is accessible in your code as $_GET['number']
To access any GET variables, use $_GET['varname']
Rather than pass a GET variable everytime you redirect, consider using a cookie (setcookie function and $_COOKIE variable) or session variables (start_session function and $_SESSION variable)
Question related to PHP memory-handling from someone not yet very experienced in PHP:
If I set a PHP session variable of a particular name, and then set a session variable of the exact same name elsewhere (during the same session), is the original variable over-written, or does junk accumulate in the session?
In other words, should I be destroying a previous session variable before creating a new one of the same name?
Thank you.
$_SESSION works just like any other array, so if you use the same key each time, the value is overwritten.
Tom,
It depends on how you use the session variable, but it generally means "erasing" that variable (replacing the old value by the new value, to be exact).
A session variable can store a string, a number or even an object.
<?php
# file1.php
session_start();
$_SESSION['favcolor'] = 'green';
$_SESSION['favfood'] = array('sushi', 'sashimi');
?>
After this, the $_SESSION['favcolor'] variable and the $_SESSION['favfood'] variable is stored on the server side (as a file by default). If the same user visit another page, the page can get the data out from, or write to the same storage, thus giving the user an illusion that the server "remembers" him/her.
<?php
# file2.php
session_start();
echo $_SESSION['favcolor'], '<br />';
foreach ($_SESSION['favfood'] as $value) {
echo $value, '<br />';
}
?>
Of course, you may modify the $_SESSION variable in the way you want: you may unset() any variable, append the array in the example by $_SESSION['favfood'][] = 'hamburger'; and so on. It will all be stored to the session file (a file by default, but could be a database). But please beware that the $_SESSION variable acts magically only after a call to session_start(). That means in general, if you use sessions, you have to call session_start() at the beginning of every page of your site. Otherwise, $_SESSION is just a normal variable and no magic happens :-).
Please see the PHP reference here for more information.