Jump to content

Where to use session_start()


Recommended Posts

I thought I had this down, but I have a situation that isn't working.  I have a page for dealing with schedules called adminSchedules.php and right at the top I put

<?php  session_start(); ....

Then later on I make a xmlHTTPRequest and use the $_SESSION to get & set some session variables. I echoed them back to be sure they were set.  Then I go back to the main adminSchedules page and I make another xmlHTTPRequest and when I try to access the session vars I set before, they are not set.   I've checked over all the syntax carefully and the error I'm getting in the error_log file is 'variable undefined '_SESSION' .    So I'm confused and thought maybe the sessions don't extend to xmlHTTPRequests or what.  Can someone explain exactly what the 'scope' of a session is, or what I should be doing differently?  I do understand about when you invoke a session var the compiler looks on your computer for the session key and all that, so I don't understand why it's not returning the  value when I ask for it.  should I be putting a 'session_start()' at the top of every PHP file that I make an xmlHTTPRequest to?  Thank you so much for your help

Link to post
Share on other sites

OK, I will keep that in mind, but the thing that seems conflicting is that I used the session_start() only once at the beginning of my main page and then the session worked fine in the first http request but then bombed in the second.  I've also read that if you invoke the session_start in more than 2 places it can screw things up.  So you're saying I do need a session_start() at the beginning of each php code I invoke with an HTTP request.   I'll give it a whirl.  Thanks for the help!

 

Link to post
Share on other sites

That seems to have done the trick.  Now I have one other item concerning syntax that I hope you can help me with.   I have a button with an onclick function and in that function I want to set a php session to the option that was chosen.   

function saveTheValue(theSID){

'<?php $_SESSION['editSchedStand']=' + theSID + ';?>';

}

Can you tell me if this is possible or show me the correct syntax?  Thank you

Link to post
Share on other sites

Its regrettable to say, but unfortunately PHP code is run, evaluated and removed before it leaves the web server. And because JavaScript only runs on the Client machines, there's no chance that JavaScript and PHP can interact beyond a server communication method.

All you're sending to the browser is

function saveTheValue(theSID){

;

}

If you wish to access data that's in your PHP (or set things in your PHP), you'll need to make an xmlHTTPrequest to do so.

Link to post
Share on other sites

I've also read that if you invoke the session_start in more than 2 places it can screw things up.

If you try to start the session while it is already started then PHP will show an error.  If it hasn't been started for that request then there's no problem.  Remember, every HTTP request is separate in PHP.  Starting a session in one request does not automatically start it in any other request.  It doesn't matter if it is an XHR request, or typing a URL and hitting enter, or clicking a link.  They're all just requests.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...