5 Replies - 2004 Views - Last Post: 25 February 2011 - 03:00 PM Rate Topic: -----

#1 dameon51  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 73
  • Joined: 07-June 10

php sessions and ajax

Posted 25 February 2011 - 02:36 PM

Hi everyone. My companies CMS relies heavily on AJAX. The only way I know of to access your session when doing a call with ajax is to do a session_start() before you access your $_SESSION object. I have started encountering problems with this, since if you don't check if your session is started php gives a notification. I found that these notifications were causing some of my sites to not work in ie 7 and 8, even when suppressed (the sites simply did not load consistently). I added php_flag session.auto_start 1 to an .htaccess file, but it only works when the script is not called with AJAX.

I would prefer to get away from doing session_start()'s all over the place, as it seems redundant, especially since I need to check to see if there is a session before I do the session_start().

Is there something I can do on the server to automatically create\handle sessions even when AJAX calls the php?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: php sessions and ajax

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: php sessions and ajax

Posted 25 February 2011 - 02:43 PM

Why do you need to check first? Here's a snippet from something I'm working on:
<?php
require("user_class.php");
require("functions.php");
session_start();

if(isset($_SESSION['usr'])) {
	$user = $_SESSION['usr'];
	$user->connectdb();
} else {
	if(isset($_POST['login'])) {
		// check login & type
		if(1) { // successful login as admin
			$user = new admin($dsn, $username, $password);
		} elseif(1) { // successful login as a user
			$user = new myuser($dsn, $username, $password);
		}
	}
	if(!isset($user)) {
		$user = new myuser($dsn, $username, $password);
	}
}


This code can be in the index.php file and it can be in the file requested by Ajax. In fact, as I'm testing it I have the Ajax request the index.php file! No problems here.
Was This Post Helpful? 0
  • +
  • -

#3 dameon51  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 73
  • Joined: 07-June 10

Re: php sessions and ajax

Posted 25 February 2011 - 02:47 PM

you need to check or php gives a notification. These notification are turned off by default so you don't see them, but I have encountered a situation where if I have these notifications the site doesn't work in IE.

yes I know it works with ajax when you do a session_start(), but I'm trying to get away from that or I'll have session_start()'s all over the place.

any one have any idea's on how to automatically handle the session from the server even when ajax access's the script?
Was This Post Helpful? 0
  • +
  • -

#4 dameon51  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 73
  • Joined: 07-June 10

Re: php sessions and ajax

Posted 25 February 2011 - 02:53 PM

I'm thinking it isn't working as I want it because ajax doesn't pass cookies with the http request? Is there a way to pass cookies automatically with ajax, or to set php so the sessions work without cookies?
Was This Post Helpful? 0
  • +
  • -

#5 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: php sessions and ajax

Posted 25 February 2011 - 02:57 PM

Quote

I need to check to see if there is a session before I do the session_start().


You can do a check before you call session_start() but it will not give you the results you are wanting.



Not sure if this is off topic or not, but is usually important with AJAX usage. :sweatdrop:
At the top of your AJAX Page (the one running in the background) you are going to want to prevent caching.

It raises issues because its loading in the background, it will read the cache before looking for new results.

I think this is because its not a directly viewed page, if you load the page directly it will have a better chance of refreshing the cache and return proper results.

You want to prevent caching all together when running items in the background so the results are accurate.

If you want to avoid loading the session in the ajax page, append the session value to the ajax url you are processing.

xmlhttp.open("GET","process.php?sess=<?=$_SESSION['my_secret']?>",true);

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: php sessions and ajax

Posted 25 February 2011 - 03:00 PM

Well, my php.ini has this for error notices:
error_reporting = E_ALL
and I get nothing in my error log. Maybe you know of another setting that will show these warnings?

Cookies are stored on the browser. When you use session_start() it gets a cookie which tells the server which session the user belongs to, so that session data can be retrieved from the server for that particular user. This works for me without errors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1