Forum Moderators: phranque

Message Too Old, No Replies

Problem with a cookie

         

The_Kellys

7:11 pm on Dec 13, 2010 (gmt 0)

10+ Year Member


Hello,

First can I ask if anyone is kind enough to answer can they keep their answers as simple as possible. I am quite useless at this but I'm having a go anyway.

There is one specific page that I want users to access via the index page. In other words I dont want them bookmarking the page and gaining future access's that way.

I have checked out this site as well as others and cant find a suitable example, or, if I did, I did not recognise it as such.

This is what I have cobbled together so far.

RewriteCond %{REQUEST_URI} ^/index.shtml
RewriteRule ^/index.shtml - [CO=somecookiename:yes:.MYDOMAIN::/]

RewriteCond %{REQUEST_URI} ^/somedirectory/somefile.shtml
RewriteCond %{HTTP_COOKIE} !somecookiename
RewriteRule .* /someinstructionspage.shtml [L]

I cant say I understand what I am doing...if I did I would not be here asking for help. Any help offered will be very gratefully received.

Best Wishes

jdMorgan

1:45 am on Dec 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This looks OK in outline... Do you have a specific question or problem with it?

The literal periods in the regex patterns need to be escaped with a preceding backslash, and the first rewritecond is redundant, but otherwise the basic approach should work, assuming that this code goes in a server config file outside of any <Directory> sections.

Jim

The_Kellys

2:38 am on Dec 14, 2010 (gmt 0)

10+ Year Member



Hello,

Yes I see to have missed some out. My code does not appear to work properly.

The code is part of my htaccess file in the root of the site.

The way I have it in English, The first block says, If you have come in via the index page then lets create a cookie called somecookiename and give it a value yes (I dont know why I should do that) and do not set a time period so that the cookie dies when the browser closes

The second block of code tests if the page called is a specific page and if it is then it looks to see if the cookie exists and if it does not then you are sent to a page of my choosing.

At the moment if you call the specific page in the second block (somedirectory/somefile.shtml) then no matter how you entered the website you are always sent to the page someinstructionspage.shtml

So, either the cookie is not being created and the second block is working fine, OR, whether the first block works or not the second block is defective and it always assumes there is no cookie

Regarding your comment, the first rewrite is the one that creates the cookie the next tests which file was requested and the next tests if the cookie exists. Or at least thats what I want them to do, clearly at least one of them does not

Thanks for replying and I hope what I have added makes things clearer.

Best Wishes, John

The_Kellys

3:01 am on Dec 14, 2010 (gmt 0)

10+ Year Member



Hello again,

Yes I now understand what you mean about the first rewrite condition and I have got rid of it...still tinkering with it

Best Wishes, John

The_Kellys

3:49 am on Dec 14, 2010 (gmt 0)

10+ Year Member



Hello yet again,

I'm baffled. Using the tools in chrome I can see that no cookie is set. I can see that other sites are creating cookies ok. Here is the actual code I am now using. It does not work and I cant see why

RewriteRule ^/index.shtml - [CO=thewayin:yes:.the-kellys.co.uk::/]

RewriteCond %{REQUEST_URI} ^\/flatred\/fr\.shtml
RewriteCond %{HTTP_COOKIE} !thewayin
RewriteRule .* /properway.shtml [L]

jdMorgan

3:18 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your "set cookie" rule will only work in a server config file outside of any <Directory> section -- See my previous comment. For use in the-kellys.co.uk/.htaccess, the code must be:

# Set the cookie on index.shtml accesses
RewriteRule ^index\.shtml - [CO=thewayin:yes:.the-kellys.co.uk::/]
#
# If cookie is not set, rewrite requests for URL-path /flatred.shtml to filepath /properway.shtml
RewriteCond %{HTTP_COOKIE} !^thewayin$
RewriteRule ^flatred/fr\.shtml$ /properway.shtml [L]

Note that the leading slash has been removed from the first RewriteRule's pattern, and that the redundant "RewriteCond %{REQUEST_URI}" function has been moved into the second RewriteRule itself.

I *have not* examined the format of your cookie declaration in detail. If you still have cross-browser issues with the code modified above for use in the-kellys.co.uk/.htaccess, then may sure that the cookie is being properly defined. Use of "<dot>the-kellys.co.uk" is correct if you intend this cookie to apply across all subdomains, but I have not looked into the effects, meaning, or correctness of the preceding ":yes:" or the trailing "::/".

Other minor tweaks for proper pattern escaping and anchoring are for "completeness" only, and should not directly affect the problem at hand.

You may wish to use a server headers checker such as the "Live HTTP Headers" add-on for Firefox and Mozilla-based browsers. This will allow you to watch the client request/server-response HTTP transactions during testing, and to see the actual content of the HTTP "Set-Cookie" server response headers and HTTP "Cookie" client request headers, etc. Such a server headers checker is basic Webmaster kit.

Jim

The_Kellys

4:06 pm on Dec 16, 2010 (gmt 0)

10+ Year Member



Hello,

Frankly I don't know what I would do without your help. I have adopted your corrections and as if by magic it works.

The : character was used in several examples I dug up elsewhere.

On the subject of ":yes:" From examples elsewhere I was given to believe that this had to be included and that it sets the value of the cookie to yes. So I used the example code in their example.

on the subject of ::/: from the same examples above...their example was :1440:/: signifying 24hrs and domain wide. I took a stab at no value being equal to while the browser is active (or whatever the proper name is) and it apparently works.

Once again, many thanks for your help. I have to admit I was struggling a bit.

Best Wishes, John

jdMorgan

5:01 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I took a stab at no value being equal to while the browser is active (or whatever the proper name is) ...

Properly termed a "session cookie" -- short for "current-browser-session-only cookie persistence," and not to be confused with "user sessions" as implemented by shopping carts and some other scripts (such as forums).

To be clear, I did not investigate the values you specified for your cookie, and therefore I was/am making no statement whatsoever regarding their validity or correctness.

Jim

The_Kellys

5:47 pm on Dec 16, 2010 (gmt 0)

10+ Year Member



Hello,

Yes I understand the intent of your comment perfectly. I just thought I would say how I came to use them. I do appreciate that just about any other webmaster would have a greater requirement on the specifications of the cookie. For me what I have works well even though some of the code may not be perfect.

Best Wishes, John