I have a main page that starts a PHP session, which creates session ID #1. I check this with session_id(). I want to access these session variables from a PHP AJAX script. To test this i just simply execute:
session_start();
echo session_id();
from this AJAX script. It returns a different AJAX session ID.
I know I can pass the session ID from the main page to the AJAX script. Is there another way to make the AJAX script open the same session by default?
For clarification I am doing this via javascript, more specifically it is the add/edit script for JQGrid.
This is simple.
in ajax, post your session_id to the ajax php.
in ajax php set it like: session_id($_REQUEST['sid'])
then you are in the same session
Related
A project I'm updating was written in CodeIgniter. I am trying to pull information from the database using the standard CI methods of...
$this->db->select('myfield')...
However, $this is not defined in my ajax page. It is a separate simple PHP page that is just going to pull the info I need and return a HTML table of the contents.
How can I access $this from the ajax script? I have a function in my model file too, and I have tried to access it from the ajax script using
$this->my_model->myfunction();
but that fails as well. CodeIgniter is version 2.1.2
The easiest way to access CodeIgniter's methods and its $this object is to be inside of a controller function.
Don't make a standalone PHP page, instead make a controller (or a new function in an existing controller) and send your AJAX request to there. You can send POST variables and access then via $this->input->post('var').
I am planning to validate session for secure content using ajax. This will prevent access to secure page after logout or using browser cache.
Is this right approach to implement java script in html body tag to validate the session for every page load?
I tried with $( document ).ready(). But the browser cache retains the old value which says session alive.
Any suggestion on this?
The usual approach is to assign the session value to a method inside your controller. Then when you require the session value, call the method.
When I'm logging in with my CakePHP login function in the Users controller using ajax. The errors will be displayed in an Div above the login form. But when I have a successful login, I want to redirect the page to the homepage. When I'm using $this->redirect('home'); in the Users controller, the Div will only be updated.
How can I make sure that the entire page will be reloaded? I rather fix this problem with PHP than Javascript 'cause I'm using the Writebuffer method with the JSHelper.
Thanks!
This should be solved in client side. Because client browser gets the AJAX response and puts the output inside the DIV.
Although you can embed javascript code inside the AJAX response. For example, if login is incorrect, then put a window.location javascript code inside the output. But this is not a good solution.
Return an HTTP status code within AJAX response. If for example code is 401, then redirect.
if (data == '401') {
document.location.href="http://example.com";
}
I've created a session variable and stored a value "123456" in it.
I need that value on another page that is called using ajax. I cannot access the session variable when making the ajax call. session_start() is in top of both pages. I even tried to write the actual session value to a txt file from the page that the ajax function calls, but the file turned out blank.
What to do?
You cannot access PHP session info from Javascript (I'm assuming that this is what you're trying to do). You can pass it in as a hidden field or in the JS (dynamically added with PHP) to the second page, add it to a regular cookie, or provide it from PHP as a response to an AJAX request, but I think those are your only options.
I'm wondering if I can use the header("Location: someFile.php"); from a script that is called via AJAX. When I am calling this script the response is the page I am attempting to redirect to.
If not, what are some options to handle redirecting users via an ajax call?
I've tried window.location() but it does not capture browser history (can't use the back button).
No, if you issue header("Location: ...") - you will redirect an AJAX call itself, but not the user's browser window.
The only way is to window.location.href="http://my.new.location/xxx/yyyy"
And the "back button problem" is common for a whole AJAX thing.
Hovewer, setting a window.location.href from a pure javascript (not AJAX) makes browser 'back' button work as expected. You could try with AJAX.