开发者

Trouble with 2 session_id()'s on my site - PHP/Jquery

开发者 https://www.devze.com 2023-01-07 20:58 出处:网络
I\'m trying to build a site that runs without refreshes, using Jquery and PHP to populate divs that slide in/out. basics work OK, but I am having trouble passing session values around.

I'm trying to build a site that runs without refreshes, using Jquery and PHP to populate divs that slide in/out. basics work OK, but I am having trouble passing session values around.

I have a login panel first that captures "$user" in PHP and then stores is as a session variable $_SESSION['user'].

I can then use it on the first page OK (the first page gets written by index.php.) Subsequent pages are called by Jquery, like:

$(".scrollclick").click(function () {
var show = this.getAttribute("id");
$("#page2content").load("showpage2.php?show="+ show);
$("#homepagecontent").hide("slide", { direction: "left" }, 1000);
$("#page2content").css("visibility","visible");           
$("#page2content").show("slide", { direction: "right" }, 1000); 

where the variable "show" comes from a clickable div image name. Again all works fine.

However, the problem is that somehow the session variables are not the same in the other PHP page's that get loaded, after the initial home page load.

I know to use session_start(); at the top of each PHP script and I've echo'ed the session_id() to prove that sessions are working and that strangely the session ids are different.

I've even got other scripts populating the session variables quite happily in the other scripts.

I seem to have proved that every other PHP script on the site creates pages with the SAME session id, except the home page which holds a different one. Trouble is, that it's the home page session that holds the "user" variable that I want to propagate.

I've tested it in Chrome, IE and FF. All the same. Result is two session id's - one for the home page an开发者_StackOverflowd one for all the others.

I am guessing that there's something in one of the other pages that is somehow flushing the session, like session_write_close() but I can't find it. I've created the site using a couple of ready made (great) scripts (Jquery chat, tutorialzine login demo), so they might be playing around.

I have just today set up a couple of dummy pages that do roughly the same thing and they work OK, so there HAS to be a really dumb mistake somewhere!!!

Possible workaround?

I even tried finding a way to manually propagate the userid across the PHP calls, but struggled to get the variables into Javascript (for the jquery calls) [I can get this to work OK] and then back into the PHP?user=xxxx string so that I can _GET it back at the other end. [can't get this to work]

I've seen some advice that suggests using:

var username = <?php echo $user;?>;

Some suggest quotes, some don't. With quotes, it's just interpreted as a string, without it seems to just stop the javascript dead in its tracks

Does anyone have any pointers to either the sessions issue or the variables workaround ??

The site structure is all flat btw, all (most) in one directory in one domain.

Ever grateful! (as someone who's now wasted 3 days going round in circles)

TonyJ


PHP's session implementation relies on a single cookie, my guess is that the browser isn't passing along the cookie correctly or the response is being cached and reusing a previous session cookie. Try using this to disable caching:

$.ajax({
    url: "...php",
    cache: false,
    ...
});

If you haven't already, watch the AJAX requests with Firebug and check that the session cookies are being sent properly.


i would try and debug the pages, enter something similar to the following then save it, use the site and watch where it appears, just change the 1)- to 2,3 and so on to allow you to see which one is showing if you enter more than one on a page.

echo "1)-".$_SESSION[{yoursessionvariable}];

I would do this especially before one of the plugin calls and after. Just use the site and wait to see at what point it fails.

0

精彩评论

暂无评论...
验证码 换一张
取 消