I am working on app. Where I set some session value using ajax call. Later on without page changing I call another method where I use the session but it didn't work. I am calling both functions using ajax. First Ii call set method while after that I call get, but on get I didn't get the session value. What can cause be?
public function getmysession()
{
if($_SESSION['mahmood']=='getit'){ echo "this is set session";}
}
public function setmysesion()
{
$_SESSION['mahmood'] = 'getit';
}
You need to initialize the session at the top of your script. So at the top of your file, put:
session_start();
Related
I need to use a session that set in system call after performing mass data generation.
Is it possible to call a session that sets in system call?
Am using the following code:
system_call.php
session_start();
$_SESSION['global_notification']= 'Message';
front_end.php
session_start();
echo $_SESSION['global_notification'];
you cannot call the session variable that set in system call. you can create a file and can get the data you saved or you want to call from session variable.
like->
fwrite($file,"Added data")
and you can read the file and display the result
i'm trying to make a login page in php using ajax and php functions..
i'm trying to create a session inside the function. and i fail to make it work.
this is basicly what i'm trying to do
the ajax code is
$.post("process.php?do=createSession",
function(data) {alert("Data Loaded: " + data);});
in process.php i got this script
include ( 'bod_function.php' );
$process_func = $_REQUEST['do'];
echo BODeal::$process_func();
and in the bod_function.php i got this running
class BODeal {
public static function createSession() {
session_start();
$_SESSION['test']='Success';
return 'yeah';
}
}
i got the alert box running and saying 'yeah' but $_SESSION['test'] is empty. no session has been created.
The session needs to be started in each new page you open. I think what you want to do is start the session from the page you are calling from and leave it empty. Then when you make your AJAX call you will need to start the session again, as you have, and then set:
$_SESSION['test']='Success';
When you have a session in PHP each page that is included in the session needs a call to session_start() before the global $_SESSION can be accessed.
i need to avoid the creation of the session in the application unless it is completely necessary.
I have noticed that xajax calls dont work properly if the session is not started :(. My first approach was to create the session (if it doesn't exist) at the begining of the xajax function, however, it doesn't work the first time the user invokes the call (it works the second time since the session was created).
There is any way to handle/fix this situation?
Edit: an example code:
function example ($parameters) {
if (!isset($_COOKIE["PHPSESSID"])) {
session_start(); // we create the session if it didn't exist previously
}
$response = new XajaxResponse();
.....
return $response;
}
My idea is to create the session when the user makes an ajax call. With this situation, the first time i call the "example" function it doesnt work. The second it goes ok, i think because the session was created.
EDIT: Hello, i have noticed a problem under chrome and explorer :(. The first ajax call is not received (i get not answer). Than means the user needs to click twice in order to receive the properly answer (with a popup for example)
Thanks!
The issue seems to be the fact that you are not calling session_start() if $_COOKIE['PHPSESSID'] is set, and consequently the session is not initialized for the current ajax request. You must call session_start() on every script that uses the session -- it isn't just for session initialization.
function example ($parameters) {
// If this function uses the session, you MUST call session_start()
// Don't do it conditionally.
session_start();
$response = new XajaxResponse();
.....
return $response;
}
If all your ajax handler functions make use of the session, then you might as well just call session_start() at the top of the file that contains them. If you don't want the session loaded prior to ajax calls, then segregate them into their own PHP script, while you do not call session_start() on the main script they initially load.
For session you have to reload page without reloading page session can not create.....
And Ajax is useful for without reloading the whole page.
Ok, this is starting to annoy me, as it's quite simply and works elsewhere, but on this current task it doesn't, so here I go!
There is a main page which relies on either a session variable being set or not to display certain information.
Let's say this page is located here: http://dev.example.com/some_page.php
e.g.
if (isset($_SESSION["some_var"])) { /* it's set so do whatever */ }
else { /* not set so do whatever else.. */ }
There is an ajax page triggered by jQuery $.ajax() to call and set this session variable to null to change the action of the main page, let's say it's located here: http://dev.example.com/ajax/some_ajax_page.php
It's code looks like so:
<?php
if (!isset($_SESSION)) session_start();
$_SESSION["some_var"] = null;
When the main page is reloaded after the ajax is triggered, the session var "some_var" is still intact, but if it's echoed after the "null" in the ajax page then it is set to "null".
Basically it doesn't seem to write to the global session, only to the local path.
Does this make sense?
Any help please? Also if you want more clarification with anything let me know!
The session_start() function will handle the attempt to create and persist a session for you, as defined by PHP's configuration, or optionally at runtime if you set your own save handler. Make sure you read the documentation here:
http://us2.php.net/manual/en/function.session-start.php
For your code, you want to make sure to call session_start() at the beginning of any page in which you'd like to save or access session variables. So your page above may look like:
<?php
session_start();
$_SESSION['myvar'] = 'some value';
Then in a different page you can try to access that value:
<?php
session_start();
if ($_SESSION['myvar'] == 'some value') {
// do something
}
That should work fine.
Get rid of the check for session. If this is the only file your calling just do this:
<?php
session_start();
$_SESSION["some_var"] = null;
Also, are you using framework that auto-regenerates session ID on each request? If so, you'll might have problems.
If you have a dev machine to play with and permissions to do so, you can manually delete all sessions in the /var/lib/php/session/ directory. As you use your site, only one session file should be created. You can also inspect that file to see what is getting written and when.
Seems that you are using different sessions vars. One for the AJAX call and another for the normal pages calls. This may occur when you do not init both call in the same way (or using the same starting code that initializes the sessions)
Be sure to session_start() both calls using the same session_id.
// try in both calls
session_start();
echo session_id(); // must return the same id in both calls
Why don't you use unset? It is the proper way to do it.
Turns out the application I was working on had it's own session_handler and if it was not included before requesting the session data, it was always invalid, eventhough it was the same session_id.
For logging out a user from my website, I am redirecting the page to logout.php where I am using session_destroy() function. Even there also, logout functionality is not working without session_start() function. By adding session_start() function before session_destroy() function, I am able to logout the user successfully.
Why do I need to use session_start() function everytime and in every page where I am doing something related to sessions?
session_destroy() destroys the active session. If you do not initialized the session, there will be nothing to be destroyed.
Why do I need to use session_start() function everytime and in every page where I am doing something related to sessions?
So PHP knows which session to destroy. session_start() looks whether a session cookie or ID is present. Only with that information can you destroy it.
In the default configuration, PHP Sessions operate off of the hard disk. PHP asks you to explicitly tell it when you need this support to avoid unnecessary disk IO.
session_start() also tells PHP to find out if the user's session exists.
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.
as per http://php.net/manual/en/function.session-start.php
Essentially by calling session_start(), PHP reads the header and cross references that session ID to what is on your system(file system/database/etc), which can then populate the $_SESSION that is relavent to that specific user. Which in turn allows you to call session_destroy() because it knows what session to actually destroy.
consider session_start() as your way of telling the php engine.... that you want to work with sessions.
and, as i understand it, always make that to be the first line ever in php page.
I was confused with the usage of session_start(); and every time I was using a session variable, I was calling session_start. Precisely, I had session_start(); more than once on each page (without even calling session_destroy()). For example,
// 1st call
session_start();
if (!isset($_SESSION['UserID']))
{
// Do something
}
else
{
// Do something else
}
// .... some other code
// 2nd call
session_start();
if (!isset($_SESSION['UserID']))
{
// Do something totally different
}
else
{
// Do something else totally different
}
This was creating a performance issue for me. So I ended up calling session_start(); just once at the very top of the page and everything seems to be working fine.
You have to call session_start once (and only once) in every file you want sessions to work in.
A common approach allowing you to only call it once is to have a dispatcher file as your index.php; call session_start in here and have this page include others based on the url's $_GET.
<?php
session_start();
if(isset($_GET['page']) && file_exists('pages/'.$_GET['page'].'.php') {
include $_GET['page'];
}
?>
//www.mysite.com/index.php?page=fish will display /pages/fish.php with session access