No, session variables are stored SERVER-SIDE. They are held in a simple array on the server as all PHP arrays are.
There are no session variables save inside a browser in any manner!
Let’s explain my comments further. PHP is SERVER-SIDE only. Nothing in PHP exist at all in any browser.
PHP is executed on the server before the browser knows it exists. Before the page is sent to the browser,
all PHP code is stripped from the page and the rest, including PHP outputs are sent to the browser. All of the
session info is saved SERVER-SIDE only. When a browser connects to a webpage, it is attached to the server
using a form of session info. Actually, the PHP server handles this and add all of the info into the session array.
This session array contains many many items depending on the server’s set up. This include IP address and
other such info.
You may manipulate session arrays and variables, save them, delete them, BUT only SERVER-SIDE. Once
the info is sent to the browser, all that is on it is HTML, Javascript and other CLIENT-SIDE programming tools.
You can prove this fact by VIEW-SOURCE any page and look for PHP code… There will be none. It is all on the
server. (One exception, you can print or echo PHP code into a HTML display to show code to people, but, this
is NOT PHP code, just plain text.
Now, on to cookies… Cookies are CLIENT-SIDE only. They are not saved on a server, they are saved on the
local machine attached to the webpage. That is why they are not usually used for anything having to do with
security as they can be changed by any smart programmer. They are usually used to save a small note such
as what page the user was on last, the time they last logged in, something similar. They are small text files
and can contain encrypted text info. So, you could encrypt a user name and if it matches, allow them in with
no user name check. But, again, security issues with that.
Now, Panther, you have one small issue with your file titel2.php. Sessions should only be started once. You
start the session at the top of the page as you should. Then, inside your “include” you start it again. This can
cause the session to be altered and might be causing an issue. If not, you must show us how you are storing
your cookies as that is most likely where the errors are hiding.
Oh, also, you usually never close a session unless the user log’s out. (Or times out from lack of use.)
If you keep closing sessions and reopening a new one, the server wastes a lot of time recapturing your IP
address and all of the other session values. Unless you are tracking the session ID’s for some odd use of
security. Usually, you create one session using session_start; and use this again at the top of every page
that needs to access session variables and then close it out when the user logs out. You can set a time
limit on a session and have it close after that amount of time so they must log back in. Also, when a user
closes his/her browser, it drops the session.
Hope this “book” doesn’t mix you up further, but, it is most likely a cookie issue not session issue.
Also, it is very easy to check for cookie’s being enabled, but, not in PHP… PHP is SERVER-SIDE only and
does not see the browser at all. BUT, Javascript does. It can read the browser’s settings and alert the
user to turning on their cookies… (You actually can do it server-side with perl or cgi, but…)
Here is javascript sample for checking cookies…
function are_cookies_enabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? true : false;
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
{
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
}
return (cookieEnabled);
}
You can call the above function using a Javascript “onload” command and check for the results.
Then, send an alert to the user telling them to turn on cookies…
One problem with this is it is Javascript. So, you might have to check to see if Javascript is turned on.
(Some companies do not allow Javascript…)
I never use cookies, I just store that data in a database and force users to log in each time. Much more
secure and never have to rebuild a cookie when the user deletes all theirs…
Hope this helps and wasn’t too long a note!