26286 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Bunging servers in
disk arrays
achieves nothing.
There, I said it
BMW offers in-car
streaming music for
cross-Europe road
trips
NetApp: We laid off
100s, profits dived
- and it"s all YOUR
fault
Ed Miliband brands
Google"s UK tax
avoidance "WRONG"
Yahoo "won"t screw
Tumblr"? Then
Tumblr will screw
its balance sheet
Slim Shady wannabe
Zuck"s Facebook
"STOLE" MY SONG -
Eminem
Stand back,
everyone! Dragons"
Den ace HAS FOUND
THE CLOUD
Blue Coat gobbles
CCTV-for-network-tr
affic maker Solera
Word 2 to Office
365 and beyond: The
good, the bad and
the Ribbon
If you"ve bought
DRM"d film files
from Acetrax,
here"s the bad news
Slashdot
Debian GNU/Hurd
2013 Released
Xbox One: No
Always-Online
Requirement, But
Needs To Phone Home
Ask Slashdot:
Moving From
Contract Developers
To Hiring One
In-House?
Quadcopter Drone
Network Will
Transport Supplies
For Disaster Relief
Congressional
Report: US Power
Grid Highly
Vulnerable To
Cyberattack
Google Chrome 27 Is
Out: 5% Faster Page
Loads
Special Ops Takes
Its Manhunts Into
Space
Aurora Attackers
Were Looking For
Google"s
Surveillance
Database
Dart Is Not the
Language You Think
It Is
EPA Makes a Rad
Decision
Article viewer

Advanced: Sessions



Written by:dimport
Published by:thinkt4nk
Published on:2003-06-21 07:19:46
Topic:PHP
Search OSI about PHP.More articles by dimport.
 viewed 12550 times send this article printer friendly

Digg this!
    Rate this article :
In this small tutorial i'll cover three different types of assigning a user a session id while he surfs around your website. These solutions are different of the two PHP built in (trans-sid and cookies).

You should have a basic knowledge and understanding of sessions and session handling in PHP for this tutorial.Here we go,i suppose you know the two methods how PHP can transport the session id from one page to another


Cookies:

the session id (sid) is stored in a cookie on the users computer and deleted automatically when the user closes the browser (-> leaves the site) If PHP4 is compiled with the --enable-trans-sid option you can set session.use_trans_sid to 1 in the php.ini to force PHP to automatically add the PHPSESSID parameter to every url.These methods are definetly not the best because they either require the user to have cookies activated (which probably 20% of them have not) or PHP needs to grep each file for <a links to add the &PHPSESSID=4efw... parameter to each url.I'll show you 3 quick and clever methods to circumvent most of the negative aspects of PHP's built in session id handling:1. Dynamic PathsHave you ever been on amazon.com (i bet you have) and checked your url? It looks similar to the following: I guess you already noticed that the session id is put after the filename at amazon.com; these concept works because when the webserver finds a file (in our case home.html) it doesn't look at the further characters in the url any more. But the session id is there.To access the session id you have to parse the path manually. Take a look at this little code snippet:

function session_start_from_path(){ global $HTTP_HOST,$REQUEST_URI; ereg("(.{32})$",$REQUEST_URI,$regs); $session_id = $regs[1]; if(!isset($session_id) || empty($session_id)) { srand((double)microtime()*1000000); $session_id = md5(uniqid(rand())); $destination = "http://$HTTP_HOST$REQUEST_URI/$session_id"; header("Location: $destination"); } session_id($session_id); session_start();}

Use this function instead of your session_start() function and it will check if the url contains a trailing session id, if not it generates one and redirects to itself but with the url containing the session id.


Dynamic Paths with mod_rewrite

Another approach is to generate dynamic paths with apache's mod_rewrite module, resulting in url's like that:http://www.ca-osi.com/8fe9w8f98eaw98fa9f983/submit.phpFor this method you need to tweak your apache configuration a bit. Insert the following lines into your apache configuration file for the main host:

RewriteEngine OnRewriteBase /RewriteRule ^[0-9a-z]{32}/(.+) /$1

This will cut the 32 character session id from the url and send the query to the webserver, letting the user's url untouched.Please refer to the apache/mod_rewrite documentation if you don't know how to activate mod_rewrite.So we get another function, slightly different from the one before, to extract our session id from the url:

function session_start_from_rewrite(){ global $HTTP_HOST,$REQUEST_URI; ereg("/([0-9a-z]{32})",$REQUEST_URI,$regs); $session_id = $regs[1]; if(!isset($session_id) || empty($session_id)) { srand((double)microtime()*1000000); $session_id = md5(uniqid(rand())); $destination = "http://$HTTP_HOST/$session_id$REQUEST_URI"; header("Location: $destination"); } session_id($session_id); session_start();}

Simply use this function instead of your session_start() call. These function does the same as the one mentioned above except for the mod_rewrite url
style.


Dynamic Hosts

The last method i'd like to show you is using dynamic hosts for transporting your session id. This works only on webservers where dns wildcards are set, e.g. bleh.company.com, m00.company.com ... all point to company.com or www.company.com. This'd looke like that in your nameserver configuration:

*.company.com IN A 194.29.41.112

Already got the point? Yeah, we can now have urls in the form: http://fa98fe99efu39f9ue9wf.company.com/test.phpWe can now modify our function to start a session from a dynamic host sid url:

function session_start_from_host($host){ global $HTTP_HOST,$PHP_SELF; ereg("([^\.]{32})\.",$HTTP_HOST,$regs); $session_id = $regs[1]; if(!isset($session_id) || empty($session_id)) { srand((double)microtime()*1000000); $session_id = md5(uniqid(rand())); $destination = "http://$session_id.$host$PHP_SELF"; header("Location: $destination"); } session_id($session_id); session_start();}

Just use this function as described before.These simple tricks can help you transporting your session ids without changing every link in your scripts.Thanks for reading my small tutorial,elybis[elybis@chowned.us]

This article was originally written by elybis

Did you like this article? There are hundreds more.

Comments:
Anonymous
2009-08-18 21:28:54
I like to write, can i quote?

kapali turbanli porno turbanli porno.
porno porno sitesi.
adult porno star adult filmler star
amator liseli sikis liseli siki&#351; izle.
turk amator porno filmler amator sikisler..
iri memeli sarisin sex porno izleme sitesidir.
fantezi porno sikis fantezi porno siki&#351;ler.
porno tube siki&#351; tube..
program download program download sitesi.
web tasarim...
ObatAsamUrat
2011-06-16 07:37:46
Appreciation for your current wise assess. Us along with our next door neighbor are actually only getting ready to comprehensive a number of seek about it. We have a new use a new book from my community selection nevertheless I do think My spouse and i figured out far more because of this article. Were genuinely delighted to view these kinds of exceptional specifics getting distributed unhampered available.
kaos distro
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
elasolova
My PHP Projects on Sat 26th Sep 10am
I have been developing PHP applications for almost a year now. I have developed three projects. One is a simple trivia game. The other is a question-answer based community at http://www.javaist.com/quans . The last one is a programming challenge site just
countll
Blog entry for Thu 25th Oct 7am on Thu 25th Oct 7am
soo nu on this wicked world of NET. just decided to dive in today..hope friend aroun here can help

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Test of experience (hopefully) by AcidIce

Things you're only likely to know if you've actually written a lot of PHP before :)


     
Your Ad Here
 
Copyright Open Source Institute, 2006