473,563 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sessions - with or without cookies

I've instituted a sessions based scheme on my web site to combat hot
linking to my images. When someone requests a page at my site, I set a
session variable. I then use htaccess to redirect *all* image requests
to a Php script that checks for that variable before simply delivering
the image. Direct links to my images will fail this test and no image
is served.

I am monitoring my script by sending emails to myself and finding that
this session variable is sometimes not set for what appear to be real
visitors to my pages (my page is the HTTP_REFERER ).

My first thought was that people were spoofing the referrer to look like
a request from my page (which I figured would have to be very - even
extremely - rare). On another hunch, I tried disabling cookies in my
browser and I got the same result. There is no session variable.

On my shared server:
session.use_coo kies = On
session.use_onl y_cookies = Off
session.use_tra ns_sid = 0

I thought this meant that if a visitor has cookies disabled, the server
would send the session ID in the headers somehow (vague as my
understanding of this is), but I am not finding that to be the case.
There are several visitors every day that appear to be at my site, but
no session var has been set (so my script does not serve the images -
d'oh!).

I tried setting use_trans_sid, but I agree with the warning at Php.net
(that people will bookmark or email the URL with the session ID in it).
And I'd really rather not tack PHPSESSID=nnnnn nnnnnnnnnnnnnn onto URLs
..... .... and .... ..... that didn't even work anyway (??).

Am I mistaken? I thought I could use sessions with visitors regardless
of their cookie settings.

Is there a way to insure that every visitor to my pages will, indeed,
return a session ID with further GET requests (for the images)?

--
*************** **************
Chuck Anderson • Boulder, CO
*************** **************
Jun 8 '07 #1
8 2748
At Thu, 07 Jun 2007 21:12:26 -0600, Chuck Anderson let h(is|er) monkeys
type:
I've instituted a sessions based scheme on my web site to combat hot
linking to my images. When someone requests a page at my site, I set a
session variable. I then use htaccess to redirect *all* image requests
to a Php script that checks for that variable before simply delivering
the image. Direct links to my images will fail this test and no image
is served.

I am monitoring my script by sending emails to myself and finding that
this session variable is sometimes not set for what appear to be real
visitors to my pages (my page is the HTTP_REFERER ).

My first thought was that people were spoofing the referrer to look like
a request from my page (which I figured would have to be very - even
extremely - rare). On another hunch, I tried disabling cookies in my
browser and I got the same result. There is no session variable.

On my shared server:
session.use_coo kies = On
session.use_onl y_cookies = Off
session.use_tra ns_sid = 0

I thought this meant that if a visitor has cookies disabled, the server
would send the session ID in the headers somehow (vague as my
understanding of this is), but I am not finding that to be the case.
There are several visitors every day that appear to be at my site, but
no session var has been set (so my script does not serve the images -
d'oh!).

I tried setting use_trans_sid, but I agree with the warning at Php.net
(that people will bookmark or email the URL with the session ID in it).
And I'd really rather not tack PHPSESSID=nnnnn nnnnnnnnnnnnnn onto URLs
.... .... and .... ..... that didn't even work anyway (??).

Am I mistaken? I thought I could use sessions with visitors regardless
of their cookie settings.

Is there a way to insure that every visitor to my pages will, indeed,
return a session ID with further GET requests (for the images)?
Alas, not much help, but I have had a similar experience with a sessions
based guestbook script refusing valid messages for lack of the proper
session var being set.

Behaviour seemed too random (different browsers, addresses, times, cookies
on/off) to pinpoint exactly what caused it. Pressed for a timely solution
I then reverted to captcha usage and haven't done any more research since.

I've dealt with image/multimedia hotlinking issues solely via .htaccess

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?mydomain\. com [NC]
RewriteRule .*\.(jpe?g|gif| bmp|png|swf|wmv |mpe?g|avi)$ siteinfo.png [L]
--
Schraalhans Keukenmeester - sc*********@the .Spamtrapexampl e.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples ','oranges') < 0"

Jun 8 '07 #2
Chuck Anderson wrote:
I've instituted a sessions based scheme on my web site to combat hot
linking to my images. When someone requests a page at my site, I set a
session variable. I then use htaccess to redirect *all* image requests
to a Php script that checks for that variable before simply delivering
the image. Direct links to my images will fail this test and no image
is served.

I am monitoring my script by sending emails to myself and finding that
this session variable is sometimes not set for what appear to be real
visitors to my pages (my page is the HTTP_REFERER ).

My first thought was that people were spoofing the referrer to look like
a request from my page (which I figured would have to be very - even
extremely - rare). On another hunch, I tried disabling cookies in my
browser and I got the same result. There is no session variable.

On my shared server:
session.use_coo kies = On
session.use_onl y_cookies = Off
session.use_tra ns_sid = 0

I thought this meant that if a visitor has cookies disabled, the server
would send the session ID in the headers somehow (vague as my
understanding of this is), but I am not finding that to be the case.
There are several visitors every day that appear to be at my site, but
no session var has been set (so my script does not serve the images -
d'oh!).

I tried setting use_trans_sid, but I agree with the warning at Php.net
(that people will bookmark or email the URL with the session ID in it).
And I'd really rather not tack PHPSESSID=nnnnn nnnnnnnnnnnnnn onto URLs
.... .... and .... ..... that didn't even work anyway (??).

Am I mistaken? I thought I could use sessions with visitors regardless
of their cookie settings.

Is there a way to insure that every visitor to my pages will, indeed,
return a session ID with further GET requests (for the images)?
AFAIK, there are 2 methods of propagating PHPSESSID, and those are
cookies and the url. I don't know of a third mechanism, but if someone
does please share it with us. :)

To enable both methods, you'd have to have

session.use_coo kies = On
session.use_onl y_cookies = Off
session.use_tra ns_sid = 1

This setting would mean PHP would try to use cookies, and if those are
not available (disabled), it would automatically rewrite all the links
in the page adding ?PHPSESSID=... at the end.

However, and I've just tested this, PHP would not (at least by default)
rewrite the SRC attribute of IMG tag, unlike the HREF attribute of the A
tag which it does rewrite. That would mean, even if your PHP that does
the checking was able to get the paramaters passed through the image
url, it would not receive the PHPSESSID and thus would not be able to
access the session data for clients with cookies disabled.

I suggest the following. Write a couple of simple PHP pages like this
test1.php
*************** **
<?php
session_start() ;

$_SESSION["visited"] = 1; //or whatever your flag variable is called
?>

Click <a href="test2.php ">here</ato go to test2!
*************** **

test2.php

*************** **
<?php
session_start() ;
?>

This is a <a href="page2.php ">link!</a>
<br />
This is an image without sessid<br /><br />
<img src="image1.jpg ">
This is an image with sessid<br /><br />
<img src="image1.jpg ?PHPSESSID=<?ph p echo session_id(); ?>">
*************** **

Disable cookies in your browser, go to test.php page, click the link to
go to test2.php and tell us what you see.



Jun 8 '07 #3

That would mean, even if your PHP that does the checking was able to get
the paramaters passed through the image url...
************
I'll stress this. I haven't experimented much with .htaccess beyond
basic access control so I'm not really sure how that redirect works and
what is being passed to your php script that checks the session flag
variable and what isn't.

Jun 8 '07 #4
Schraalhans Keukenmeester wrote:
At Thu, 07 Jun 2007 21:12:26 -0600, Chuck Anderson let h(is|er) monkeys
type:

>I've instituted a sessions based scheme on my web site to combat hot
linking to my images. When someone requests a page at my site, I set a
session variable. I then use htaccess to redirect *all* image requests
to a Php script that checks for that variable before simply delivering
the image. Direct links to my images will fail this test and no image
is served.

I am monitoring my script by sending emails to myself and finding that
this session variable is sometimes not set for what appear to be real
visitors to my pages (my page is the HTTP_REFERER ).

My first thought was that people were spoofing the referrer to look like
a request from my page (which I figured would have to be very - even
extremely - rare). On another hunch, I tried disabling cookies in my
browser and I got the same result. There is no session variable.

On my shared server:
session.use_co okies = On
session.use_on ly_cookies = Off
session.use_tr ans_sid = 0

I thought this meant that if a visitor has cookies disabled, the server
would send the session ID in the headers somehow (vague as my
understandin g of this is), but I am not finding that to be the case.
There are several visitors every day that appear to be at my site, but
no session var has been set (so my script does not serve the images -
d'oh!).

I tried setting use_trans_sid, but I agree with the warning at Php.net
(that people will bookmark or email the URL with the session ID in it).
And I'd really rather not tack PHPSESSID=nnnnn nnnnnnnnnnnnnn onto URLs
.... .... and .... ..... that didn't even work anyway (??).

Am I mistaken? I thought I could use sessions with visitors regardless
of their cookie settings.

Is there a way to insure that every visitor to my pages will, indeed,
return a session ID with further GET requests (for the images)?

Alas, not much help, but I have had a similar experience with a sessions
based guestbook script refusing valid messages for lack of the proper
session var being set.

Behaviour seemed too random (different browsers, addresses, times, cookies
on/off) to pinpoint exactly what caused it. Pressed for a timely solution
I then reverted to captcha usage and haven't done any more research since.

I've dealt with image/multimedia hotlinking issues solely via .htaccess

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?mydomain\. com [NC]
RewriteRule .*\.(jpe?g|gif| bmp|png|swf|wmv |mpe?g|avi)$ siteinfo.png [L]
I am seeing very definite results when I test from my own browser. With
cookies enabled, the session var is always set.

However, if I disable cookies, what happens is this (as it appears to
me). When I enter the page (with image(s) in it), I call start_session
and set my var. When the image requests are redirected (via htaccess) to
the image server script, each call to start_session (one for every image
on the page) creates a new session (empty sessions). This makes sense,
as my browser is not sending a cookie telling the server a session is in
use. Based on that, I tried setting session.use_tra ns_sid, but that did
not change anything (which seems puzzling).

I have to enable cookies to pass the session_id from my browser to the
server (??).

I've also changed session.save_pa th to a directory in my home path
(above my web space). This makes it easier for me to track what's
happening, but has not changed anything.

I've been reading (at php.net) about using session_write_c lose():
http://us2.php.net/manual/en/ref.session.php#62486
http://us2.php.net/manual/en/ref.session.php#64525

.... but using that has not helped, either. Besides, I don't think it's
relevant. The first reference has to do with using header redirect
(which I'm not doing) and the second is about locking a session file in
case concurrent page load tries to write to it.

I think use_trans_sid could be the key .... but use_trans_sid does not
seem to be working.

Now, though, it's time for the pub where I can let my my mind wander in
a more relaxed state. Maybe I'll stumble upon some insight. ô¿Ô¬ (I do
some of my best thinking while staring mindlessly at beer taps and
swilling pints.)

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*************** **************
Jun 9 '07 #5
Denis Gerina wrote:
Chuck Anderson wrote:
>I've instituted a sessions based scheme on my web site to combat hot
linking to my images. When someone requests a page at my site, I set a
session variable. I then use htaccess to redirect *all* image requests
to a Php script that checks for that variable before simply delivering
the image. Direct links to my images will fail this test and no image
is served.

I am monitoring my script by sending emails to myself and finding that
this session variable is sometimes not set for what appear to be real
visitors to my pages (my page is the HTTP_REFERER ).

My first thought was that people were spoofing the referrer to look like
a request from my page (which I figured would have to be very - even
extremely - rare). On another hunch, I tried disabling cookies in my
browser and I got the same result. There is no session variable.

On my shared server:
session.use_co okies = On
session.use_on ly_cookies = Off
session.use_tr ans_sid = 0

I thought this meant that if a visitor has cookies disabled, the server
would send the session ID in the headers somehow (vague as my
understandin g of this is), but I am not finding that to be the case.
There are several visitors every day that appear to be at my site, but
no session var has been set (so my script does not serve the images -
d'oh!).

I tried setting use_trans_sid, but I agree with the warning at Php.net
(that people will bookmark or email the URL with the session ID in it).
And I'd really rather not tack PHPSESSID=nnnnn nnnnnnnnnnnnnn onto URLs
.... .... and .... ..... that didn't even work anyway (??).

Am I mistaken? I thought I could use sessions with visitors regardless
of their cookie settings.

Is there a way to insure that every visitor to my pages will, indeed,
return a session ID with further GET requests (for the images)?


AFAIK, there are 2 methods of propagating PHPSESSID, and those are
cookies and the url. I don't know of a third mechanism, but if someone
does please share it with us. :)

To enable both methods, you'd have to have

session.use_coo kies = On
session.use_onl y_cookies = Off
session.use_tra ns_sid = 1

This setting would mean PHP would try to use cookies, and if those are
not available (disabled), it would automatically rewrite all the links
in the page adding ?PHPSESSID=... at the end.

However, and I've just tested this, PHP would not (at least by default)
rewrite the SRC attribute of IMG tag, unlike the HREF attribute of the A
tag which it does rewrite. That would mean, even if your PHP that does
the checking was able to get the paramaters passed through the image
url, it would not receive the PHPSESSID and thus would not be able to
access the session data for clients with cookies disabled.

I suggest the following. Write a couple of simple PHP pages like this
test1.php
*************** **
<?php
session_start() ;

$_SESSION["visited"] = 1; //or whatever your flag variable is called
?>

Click <a href="test2.php ">here</ato go to test2!
*************** **

test2.php

*************** **
<?php
session_start() ;
?>

This is a <a href="page2.php ">link!</a>
<br />
This is an image without sessid<br /><br />
<img src="image1.jpg ">
This is an image with sessid<br /><br />
<img src="image1.jpg ?PHPSESSID=<?ph p echo session_id(); ?>">
*************** **

Disable cookies in your browser, go to test.php page, click the link to
go to test2.php and tell us what you see.

A worthy test. I'll try some simple scripts like that .... later (it's
late).

I think I need to go back and read this again, too:
http://nedmartin.org/site/hotlink-prevention

..... He actually comes to the conclusion that cookies must be enabled.

I think I may have hit a road block in my plan.

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*************** **************
Jun 9 '07 #6
..oO(Chuck Anderson)
>I am seeing very definite results when I test from my own browser. With
cookies enabled, the session var is always set.

However, if I disable cookies, what happens is this (as it appears to
me). When I enter the page (with image(s) in it), I call start_session
and set my var. When the image requests are redirected (via htaccess) to
the image server script, each call to start_session (one for every image
on the page) creates a new session (empty sessions).
Correct, because by default PHP doesn't rewrite <imgtags when
session.use_tra ns_sid is enabled.
>This makes sense,
as my browser is not sending a cookie telling the server a session is in
use. Based on that, I tried setting session.use_tra ns_sid, but that did
not change anything (which seems puzzling).
Have a look at url_rewriter.ta gs and adjust it as required.

Micha
Jun 11 '07 #7
Michael Fesser wrote:
.oO(Chuck Anderson)

>I am seeing very definite results when I test from my own browser. With
cookies enabled, the session var is always set.

However, if I disable cookies, what happens is this (as it appears to
me). When I enter the page (with image(s) in it), I call start_session
and set my var. When the image requests are redirected (via htaccess) to
the image server script, each call to start_session (one for every image
on the page) creates a new session (empty sessions).

Correct, because by default PHP doesn't rewrite <imgtags when
session.use_tra ns_sid is enabled.

>This makes sense,
as my browser is not sending a cookie telling the server a session is in
use. Based on that, I tried setting session.use_tra ns_sid, but that did
not change anything (which seems puzzling).

Have a look at url_rewriter.ta gs and adjust it as required.

Micha
Nice! Thanks for that. That could be just what I'm looking for. I'll
have to experiment with it later (but for now I've .... places to go,
..... people to see, .... things to do ....)

I'll post back my results.

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*************** **************
Jun 11 '07 #8
Chuck Anderson wrote:
Michael Fesser wrote:
>.oO(Chuck Anderson)
>>I am seeing very definite results when I test from my own browser. With
cookies enabled, the session var is always set.

However, if I disable cookies, what happens is this (as it appears to
me). When I enter the page (with image(s) in it), I call start_session
and set my var. When the image requests are redirected (via htaccess) to
the image server script, each call to start_session (one for every image
on the page) creates a new session (empty sessions).

Correct, because by default PHP doesn't rewrite <imgtags when
session.use_tr ans_sid is enabled.
>>This makes sense,
as my browser is not sending a cookie telling the server a session is in
use. Based on that, I tried setting session.use_tra ns_sid, but that did
not change anything (which seems puzzling).

Have a look at url_rewriter.ta gs and adjust it as required.

Micha


Nice! Thanks for that. That could be just what I'm looking for. I'll
have to experiment with it later (but for now I've .... places to go,
.... people to see, .... things to do ....)

I'll post back my results.

Okay, ... this is all pretty much in a finalized state. I do have one
question, though (I'll get to later).

(This is all about hotlinking protection using sessions - see previous
posts).

1. Since many of my pages are old (plain html), I've added a redirect
in htaccess (per directory, as I want to) to send .html requests to a
php script.

2. In that script I set session.use_tra ns_sid to 1 (On) and set
url_rewriter.ta gs to "img=src" (and only that) in case the visitor has
cookies disabled (if cookies are disabled, image request URLs include
the session ID). Then it starts a session, sets a variable, verifies
the request html file is valid, and includes that file (otherwise 404).

If the file containing the images is a Php file, I include the above in
the top of the php file.

3. In the same htaccess file (Step 1.) I redirect all image requests
(where referrer does not begin with my domain) to an image serving
script. When an image is requested, if the session var is set, I
deliver the image, otherwise I do "something else" (many options here,
but ultimately a simple ....
header("HTTP/1.1 404 Not Found"); exit;
..... is all that's needed).

After many trials, and now in this final state, the scripts seem to be
doing the job quite well. It appears that all visitors to my site can
see images (I am monitoring the results).

Here is my question, though. In the case where the visitor has disabled
cookies, my image serving script has to detect the session ID in the
$_GET array, extract it (if it is there) and set the session_id with
that value before I call start_session. The session functions do not
automatically detect it and use it (as I thought it would/should?). I
have to do that in my script. It was my impression that the session
functions would do that automatically with use_trans_sid.

--
*************** **************
Chuck Anderson • Boulder, CO
*************** **************
Jun 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4727
by: Kevin Thorpe | last post by:
I've hit a problem with sessions. If I have a document relying on sessions which has a link target=_blank then the new window inherits the session from its parent (in IE). However, if I embed an instance of IE in a VBA form in Excel and click on the link, the new window doesn't inherit the session. I have no idea why this should happen...
1
2806
by: windandwaves | last post by:
Hi Gurus I am basically sorry that I have to bother you about this. I am a PHP beginner and I have been studying sessions and cookies over the last few weeks. I have learned lots, but I am missing the big picture. Is it like this: 1. user comes to site 2. user does something (e.g. a search) that may be useful later => session
11
4920
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g., http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=Tidy7IDbDHA.2108%40cpmsftngxa06.phx.gbl) that indicate that ASP will queue up requests when they come...
13
1729
by: G | last post by:
Hello, Is it possible to send form values from PAGE1 to PAGE2, and then retain the form info for PAGE3 without using cookies, sessions or DB storage? Also, I dont want to repost page2 to page3 using hidden form fields. Just curious! Want to know how to "simply" maintain user form inputs across a web site even when security and privacy...
2
3675
by: Chris Mahoney | last post by:
Hi I'm using several Sessions in my app. When the user has cookies enabled in their browser, everything works fine. But with cookies disabled, only IE seems to remember the sessions. In Firefox and Safari, the session values seem to be "forgotten". I've looked around on Google but can't find any solutions to this problem. Plus I don't...
7
3325
by: Atte André Jensen | last post by:
Hi I'm developing a site where I'd like to store information during a users visit. So far I've been using sessions, but as far as I can tell it's not possible to control for how long a session is valid. It seems that these information are valid until the browser closes. On the other hand it's possible to set expiration time for cookies. ...
3
2718
by: Jon Slaughter | last post by:
Any pitfalls or stuff I need to worry about when working with sessions? I want to write a log file and hit counter along with a login interface and I'm trying to learn this stuff. http://us.php.net/session Just wondering if theres anything that I need to keep in mind while I work on it? Thanks,
5
5554
by: jheines | last post by:
I am trying to explain how cookies and sessions work in a class I teach, but I have hit a wall when it comes to the interaction between cookies and the state of the privacy settings in Internet Explorer. I would appreciate any help anyone can offer, please. First, consider the following very simple JavaScript function: function...
8
1832
by: Dave | last post by:
Hopefully this is an easy question for those with more experience. I have two separate programs that I want to use together on a website Program A starts first and calls session_start(). Program B is started by the user clicking on a link and it also calls session_start(). The session started by program B blows away the session...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.