""
All times are GMT +1. The time now is 09:08 PM

Welcome to the
Adept Web Community - Adept Webmaster Discussion Forums
 forums!

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our
free
 community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have not received your registration email message, please check your spam box/junk mail.

Get a FREE domain name!


That's right! You can get a free domain name (or renew existing) by just posting at our forums! For every 100th post you make, we will register a new domain for you! Join our community today!

]   Adept Web Community - Adept Webmaster Discussion Forums > Coding Forums > Tutorials & Code Snippets > PHP

Reply
 
Thread Tools Display Modes
  #1  
Old August 4th, 2007, 07:03 PM
Stanislav Palatnik's Avatar
Stanislav Palatnik
Stanislav Palatnik is offline
SuperMan
 
Join Date: Jul 2007
Location: City that never sleeps
Posts: 605
Stanislav Palatnik is on a distinguished road
Default Managing cookies in PHP

Setting a cookie

PHP Code:
setcookie ('country','USA'
Setcookie() must be called before any output is made, since cookies are sent with the HTTP headers.

Possible arguments

expiration time

PHP Code:
setcookie ('country','USA',1071448497
The third argument is the expiration time, this is expressed as an epoch time stamp ie 7 am, 15/12/2045)
If this argumetn is empty (or missing) the cookie will expire when the browser is closed.
To set a expiration time for example after one hour you would use:

PHP Code:
setcookie ('country','USA',time()+3600)

time() reads the current time
+3600 adds 3600 seconds (aka 1 hour
Path

PHP Code:
setcookie ('country','USA','','/locations/'
The fourth argument indicates that the cookie is only sent back to the server when pages are requested whose path begins with the argumented string. (in this case /locations/)

Note that the cookie doesn't have to be set from this path.

Domain

PHP Code:
setcookie ('country','USA','','','.buildtolearn.com')
setcookie ('country','USA','','','yankees260an.buildtolearn.com')
setcookie ('country','USA','','','buildtolearn.com'
The fifth argument is sent back to the server only when pages whose hostname ends with the argumented domain.
The first example would sent the cookie to all sites ending with buildtolearn.com (eg: nkzd.buildtolearn.com as well as pkzone.buildtolearn.com)
The second example only sent it to yankees260an.buildtolearn.com
The third one would only sent it to Buildtolearn.com (and not to www.buildtolearn.com or netfo.buildtolearn.com)

SSL connection flag

PHP Code:
setcookie ('country','USA','','','',1
The sixth and last argument is a flag (0 or 1). If set 1 it will only send a cookie over an SSL (secure) connection. This is handy when the cookie contains sensitive information.

Deleting a cookie

Just call setcookie with no value and a expiration time in the past

PHP Code:
setcookie ('country','',time()-86400
Use an expiration time with a couple of hours or an entire day (86400 seconds). This is in case the server and the user's computer have unmatching clocks (server in America has different time with user's computer in Germany for example)
The setcookie() for deleting has to have the same arguments that the setcookie() used for setting the cookie (except for value and time).

Reading values of cookies

PHP Code:
if (isset($_COOKIE['country'])) {
print 
"I live in $_COOKIE['country']";

Cookie values are stored in the superglobal array $_COOKIE.
The $_COOKIE only stores the value of a cookie since the browser only sends this back to the server, not the expiration, path, domain and the secure status. These can't be read.

note: the set_cookie() function doesn't change the value of $_COOKIE. So during the request in which the cookie is set, the value of the cookie can't be found in $_COOKIE
__________________
Kind regards,
Stanislav Palatnik

Adept Web Community:
Donate | Board Rules | Coding Tutorials



Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
  #2  
Old August 5th, 2007, 12:51 PM
kaisellgren's Avatar
kaisellgren
kaisellgren is offline
Root Administrator
 
Join Date: Jul 2007
Location: Finland
Posts: 532
kaisellgren is on a distinguished road
Default

Freaking neat tutorial, Stan.

I have sent this to Pixel2Life for approval.
__________________
Kind regards,
Kai Sellgren



Adept Web Community:
Donate | Board Rules | Coding Tutorials

Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
  #3  
Old August 5th, 2007, 11:39 PM
Scarface1429's Avatar
Scarface1429 Scarface1429 is offline
Senior Member
 
Join Date: Jul 2007
Posts: 77
Scarface1429 is on a distinguished road
Default

Pixel2Life is getting a lot of tutorials from here.

Thanks!
Reply With Quote
  #4  
Old August 7th, 2007, 04:42 AM
Stanislav Palatnik's Avatar
Stanislav Palatnik
Stanislav Palatnik is offline
SuperMan
 
Join Date: Jul 2007
Location: City that never sleeps
Posts: 605
Stanislav Palatnik is on a distinguished road
Default

Quote:
Originally Posted by Scarface1429 View Post
Pixel2Life is getting a lot of tutorials from here.

Thanks!
Yep, they should thank us once in a while
__________________
Kind regards,
Stanislav Palatnik

Adept Web Community:
Donate | Board Rules | Coding Tutorials



Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
  #5  
Old August 7th, 2007, 05:05 PM
kaisellgren's Avatar
kaisellgren
kaisellgren is offline
Root Administrator
 
Join Date: Jul 2007
Location: Finland
Posts: 532
kaisellgren is on a distinguished road
Default

Quote:
Originally Posted by yankees26an View Post
Yep, they should thank us once in a while
Heh

Look at here http://www.pixel2life.com/sourcesite/webadepts_com/
__________________
Kind regards,
Kai Sellgren



Adept Web Community:
Donate | Board Rules | Coding Tutorials

Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
Sponsored Links
  #6  
Old August 8th, 2007, 02:40 PM
zap_xlib's Avatar
zap_xlib zap_xlib is offline
Webmaster
 
Join Date: Aug 2007
Location: Sector 1, Cluster 1, Cylinder 1024, Multi(0)
Posts: 443
zap_xlib is on a distinguished road
Default

Quote:
Originally Posted by kaisellgren View Post
Man! you got lots of tutorials!. So.. you are a good ruby programmer too!.. that`s awesome!. I`ll try out your "Ruby on Rails" and see what i can do
Reply With Quote
  #7  
Old August 10th, 2007, 10:48 PM
kaisellgren's Avatar
kaisellgren
kaisellgren is offline
Root Administrator
 
Join Date: Jul 2007
Location: Finland
Posts: 532
kaisellgren is on a distinguished road
Default

Quote:
Originally Posted by zap_xlib View Post
Man! you got lots of tutorials!. So.. you are a good ruby programmer too!.. that`s awesome!. I`ll try out your "Ruby on Rails" and see what i can do
I am not a Ruby coder, but the other admin here (Yankees26an = Stan) has started coding with Ruby some time ago.
__________________
Kind regards,
Kai Sellgren



Adept Web Community:
Donate | Board Rules | Coding Tutorials

Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT +1. The time now is 09:08 PM.
Style By: vBSkinworks