Closed Thread Icon

Preserved Topic: PHP & cookies (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21159" title="Pages that link to Preserved Topic: PHP &amp;amp; cookies (Page 1 of 1)" rel="nofollow" >Preserved Topic: PHP &amp; cookies <span class="small">(Page 1 of 1)</span>\

 
Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-02-2002 02:50

OK, the extent of my knowledge of PHP is from the posts I've seen here. I've decided I need to use a small snippet of it for my page.

What I need to do is include three text files, in an order dependant on a cookie.

So, could someone please point out to me the syntax for reading cookies, defining variables, creating some sort of if block, and including files? Or maybe just give me a link to a beginner's tutorial where I might be able to pick these things up? That'd be great, thanks.

(BTW; giving a ".php" file extention to a file will enable PHP and keep the browser from caching it, right?)

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 09-02-2002 03:02

Slime: PHP cookies are pretty simple when compared to the JavaScript ones - check out the manual:
http://www.php.net/setcookie

___________________
Emps

FAQs: Emperor

AT
Bipolar (III) Inmate

From: Louisville, KY, USA
Insane since: Aug 2000

posted posted 09-02-2002 06:03

Slime, yes .php files will be parsed by the server, thus 'hiding' your code

Some servers are set to parse html files.

later

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 09-02-2002 09:44

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-03-2002 19:16

Setting cookies is the hard part, the link above however should be all you need.

Reading them however is easy. First the any valid cookies should become variables with corresponding key value pairs right off the bat so if you have set a cookie like so:

mycookie=myvalue

Then when refreshing the page you should instantly have a variable named
$mycookie which equals "myvalue"

For security reasons most people use the PHP array $HTTP_COOKIE_VARS['mycookie'] (or _COOKIE['mycookie'] if you have the latest version of PHP installed can't remember the exact version, but I haven't started using the new syntax yet just for cross server compatiblity issues) however just to make sure no cookie variables are being set in the query string

to include somthing depending on the cookie is really simple
if ($HTTP_COOKIE_VARS['what_page'] == "page1") {
include("/includes/my_page1.php")
} else ...

you get the drift.
For security reasons (it's really easy for people to create their own variables in scripts) It s a bad idea to actually use something like include($HTTP_COOKIE_VARS['mypage']) because this can start showing off the dirty innards of your server if someone hacks their cookie. I generally use a hash of acceptable include pages something like this

// Unset this to make sure it's only getting set by my script
unset($ok_includes)
$ok_includes = array(
'page1' => "/includes/page1.php",
'page2' => "/includes/page2.php"
)

and then I can call them like this
include($ok_includes[$HTTP_COOKIE_VARS['mypage'])

that way only the includes in my hash can get called. Also (and I have to admit I'm guilty of this) don't call include pages with php code something like "page.inc" especially if it's in your httpdocs viewable tree because if someone figures that out they can view the page. Name them .php so they get parsed and will show up blank if it's just php code. (I take this a step farther and put all include files outside of my web document tree and refrence the includes by a full server path name ie. "/var/www/inc/")





.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-03-2002 19:42

Thanks, everyone. This should be all I need.

The cookies are being *set* with Javascript, so I won't need to set them with PHP, and I probably won't use session variables for that same reason (thanks anyway InI). When a cookie hasn't been set I'll just revert to a default order.

Thanks again!

« BackwardsOnwards »

Show Forum Drop Down Menu