Forum Moderators: coopster

Message Too Old, No Replies

PHP cookies inside the web browser

PHP cookies inside the web browser

         

Mikemill

6:21 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



A while ago I figured out a way to store php session vars inside the web browser. Not a cookie on the hard drive but actually store the variable inside the web browser window so if they have cookies disabled, I can still track them in that one window. The problem is, I forgot how to do it. I looked and looked and all I found where howto's on cookie vars and not session vars. All I remember is I was able to access them using $HTTP_SESSION_VARS[var_name] And to start the session with session_start().

andreasfriedrich

7:02 pm on Feb 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at PHP´s Session handling functions [php.net].

If session.use_trans_sid is on then PHP [php.net] will add the sessionid to the URL when cookies are disabled. In any case the session data are stored on the server side and the session id is needed to identify HTTP requests, since HTTP in itself is a stateless protocol. The session id will either be contained in a cookie, i.e. in the HTTP message header, or in the URL itself.

Andreas

Mikemill

7:27 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



I already looked in the sessions section on php.net and the php4 bible and php in 24 hours. I've totally mastered storing variables in cookies. But I still can't get sessions to work right. I know theres a way to store the values inside a the individual web browser, I've done it before.

If you want to take a look at my configs, here they are. Any help would totally be a BIG help.

<snip>

[edited by: jatar_k at 8:25 pm (utc) on Feb. 24, 2003]
[edit reason] no urls please [/edit]

andreasfriedrich

7:55 pm on Feb 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For some reason the cookie_domain is set to www.victorscale.com. This should be your domain name.

Also note the following:

Please note when working with sessions that a record of a session is not created until a variable has been registered using the session_register() [php.net] function or by adding a new key to the $_SESSION superglobal array. This holds true regardless of if a session has been started using the session_start() [php.net] function.

[php.net...]

Perhaps you could post the code that is not working?

In any way the session will be stored on the server side, never in a browser window.

Andreas

Mikemill

8:36 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



Heres the code...
<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
echo $_SESSION['count'];
?>

It's like this. I need to track users to my website using php. I can't use cookies because most people have those disabled. I can't put them in the url because if someone bookmarks the site, it will also store the old cookie. A while ago I found a way to store variables using session cookies. In IE, if you goto tools->internet options->privacy tab->"advanced..." button you will notice a checkbox that says "always allow session cookies". with that checked I was able to pass variables back and forth with the server but only using the current browser window. The above code incriments fine but when you open a new window and run the php file from the diffrent window, it starts over from 0. If you go back between the windows and refresh, both values change but not with the same numbers.

andreasfriedrich

8:56 pm on Feb 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A session cookie is simply a cookie that has a lifetime of 0. It will live only as long as the browser process lives. When you open a new window in the same browser process the cookie will be sent to the server. Only if you start a new browser process you will get a new sessionid and cookie.

You might want to unset session.cookie_lifetime and session.cookie_domain.

Here is my PHP [php.net] configuration:

session.auto_start
Off Off
session.cache_expire
180 180
session.cache_limiter
nocache nocache
session.cookie_domain
no value no value
session.cookie_lifetime
0 0
session.cookie_path
/ /
session.cookie_secure
Off Off
session.entropy_file
no value no value
session.entropy_length
0 0
session.gc_maxlifetime
1440 1440
session.gc_probability
1 1
session.name
PHPSESSID PHPSESSID
session.referer_check
no value no value
session.save_handler
files files
session.save_path
/tmp /tmp
session.serialize_handler
PHP [php.net] php
session.use_cookies
On On

Using this config your code works just fine when I open a new window using Ctrl-N.

Andreas

Mikemill

9:35 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



WOO HOOO! IT WORKS! HOT DANG! It was that session.cookie_lifetime thing, it had to be set to 0 like you said. Oh man, thanks a lot bro. It's rockin now!

andreasfriedrich

9:46 pm on Feb 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got that sorted out.

Andreas