PHP session and Ajax - ajax

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.

Related

How to load view containing data with ajax?

I have Restful controller that renders a view with data from database and I want to load this view with its data in another view via ajax. There is a problem "undefined variable". Is there any solution?
When you pass variables to your view they are only available on the server side while the view renders and then they are discarded. What this means is that the variables are only available to the php of your application and then they are gone by the time the view has been rendered and sent to the visitors web browser.
If you try to use the variables with JavaScript then you are running the JavaScript on the client side (as opposed to the server side). The variables that you pass to your view do not exist on the client side where your JavaScript runs.
From what it sounds like. You are defining a variable in your controller via laravel. Then you are passing the variable from the controller to the view. The view is then rendered as html and sent to the visitor's computer (the client) which the html is then loaded and that is when the JavaScript starts to load.
That's the problem, now possible solutions.
First you could send the variable (assuming it is simple data and not like an object) to the browser through laravel flash variables which are actually stored on one time cookies on the client side. Then you use JavaScript to access the cookie and get the data then storing it to a js variable and using it in your script.
Second you create an Api to respond to your http requests and then send an Ajax request from your JavaScript to the api to get the data. Then you would store it in JavaScript and use it. This allows complex data like objects because you would use the JSON format to send information to respond to the Ajax call. While cookies are restricted to (5kb I think) there is really no theoretical limit to JSON data.
I hope that helps and I hope I'm understanding your problem.
Would need to see your javascript before anything, but usually for me this means a misspelled element id or misspelled a reference file

Is it right to check session using ajax and redirect page?

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.

Codeigniter - Session variables mysteriously disappearing

I am using Codeigniter 2.1.0 and CI_session for session handling.
I assume that this
Page A sets some variables in the session using $this->session->set_userdata().
Page A redirects to Page B
Page B is expected to retain the session variables that were set in Page A.
However this is what happens to me
Page A sets some variables in the session using $this->session->set_userdata().
Page A redirects to Page B
Page B does not retain the session variables that were set in Page A.
I have code in Page A to save and record the contents of $this->session->userdata in a log before redirection to Page B. The log shows that the values that are set in session exist.
However, using var_dump() on $this->session->userdata on Page B shows that those values don't exists.
I don't really know how this could be. I have double checked that I am not unsetting the values in Page B. It's like CI_Session is somehow unsetting them behind the scenes.
Any ideas?
EDIT: Yes, I am using the database to save session data. The field type is TEXT.
From what I understand from networks, the cookies and sessions are stored in client machine and not server. So HTTP basically adds cookies and sessions to your request headers and sends the request.
But in this case you are setting the sessions and then redirecting from the same page in server. Normal PHP might handle this differently but codeIgniter does not use native PHP sessions. (Refer http://ellislab.com/codeigniter/user-guide/libraries/sessions.html)
This is my guess. But it would be helpful if I take a look at your code.

Make AJAX PHP script use same session as main page

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

Appropriate redirection of forms that use AJAX

I have many forms that use AJAX (w/ jQuery) for validation and data submission. When a form is filled out correctly, I use window.location to redirect the page after I get an acceptable response from the PHP script. On the new page, I use a session variable (set after the AJAX calls) to display the appropriate content. Please tell me if this is standard practice or please give me some suggestions.
Thanks!
Is there a reason you would use a $_SESSION variable to store the post-submission content? Standard practice would be to validate the form via AJAX but submit it in the standard way (i.e. via $_GET or $_POST) after validation. This way you don't need to store anything to a session and you'll likely have less to debug as you'll be submitting the form and displaying its results in the most widely-accepted way.
The benefit of AJAX is typically so that you can submit the form without actually having to do the redirect/refresh. You could get the same functionality by simply having your form POST to the destination URL, redirect to the appropriate place from there or send them back to the form displaying any errors that may have occurred. You could use AJAX to validate the form before the submission to save your users a redirection back to the form to fix their errors, but this is really just a convenience for them. Also, you will have to validate any user data on the server side once it has been submitted, as you can't rely on client-side validation, so you might as well forget the AJAX validation.

Resources