Jul 25, 2008

Something I Discovered With Sessions [php]

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Something I Discovered With Sessions [php]

ghostrider
Hello All,

I've been doing a lot of PHP programming since I last posted here. I've run across two security related things with sessions that you may or may not know about.

The first one pertains to the session id, or the id that PHP assigns each computer when a session is created. This id is either stored in a cookie (search for PHPSESSID) or through the URL as GET data. Remember that all session data is stored server side; this ID is the only thing that PHP will use to differentiate your computer from someone else's. While I was programming for Plug Computers, I decided to experiment with logging into the Admin CP I built, copying the session ID from my computer, and putting it as a cookie on the other computer. Sure enough, both computers were recognized as logged into the admin CP.

This poses a very serious threat to anyone that knows what they are doing with sessions. However, there is a system that I use to eliminate this from occurring. PHP has a bunch of functions for working with sessions. I utilize the "session_regenerate_id", which will change the session ID every time it is invoked. This way, if someone is able to capture someone's session ID, the next time that function is invoked, the ID will be useless.

CODE
<?
session_start(); // Start the session.
session_regenerate_id(); // Give the session a new id.
?>


I recommend doing that every time your user loads a new page.

The second discovery I've made pertains to using local variables and session variables. When I write PHP for people I use PHP version 5.2.2. I believe newer versions have addressed and fixed this issue. But just in case, I'll share what I've found anyway.

Firstly, let me tell you what I mean by a local variable. Look at the code below:

CODE
function some_function($var) {
$newvar = $var;
++$newvar; // This is not a local variable.
return $newvar;
}

$othervar = 7; // This is a local variable


Variables within functions are not considered local variables because when a user-defined function is invoked, PHP will create the variables within that function, and then destroy them once the function has been completed. Other variables outside of functions are considered "local variables", with the exception of superglobals ($_POST, $_GET, $_COOKIE, $_SERVER).

I am currently writing some PHP for a tenis coach in Indiana who wants a way to allow his students to keep track of work out routines, keep a blog, keep notes on opponents and other things like that. When working with the Admin CP and creating a "add user" form for the site, I noticed that the key 'username' in the session superglobal would change each time I ran the script.

Here is the code in question:
CODE
$username = $_POST['username'];


The value in $_SESSION['username'] was changed to $username. It's a bug that also caused me some difficulty with Plug Computers. I had to change all of my $id's to $id1's to avoid it.

Hope this helps someone out.

 

 

 


Reply

babyboomer
I never actually thought about the session variable being a security hole. Thanx for pointing that out mate.

The thing about local and global variables must be known by every php coder. I mean this is like the abc of this thing.

Keep up posting.

Reply

galexcd
This is the main reason I don't use session in php. I write it myself, come up with a random string and make my own cookie this way I have more control over it. I check the IP address that was logged in with, with the one trying to get the request.

Reply

jlhaslip
QUOTE(alex7h3pr0gr4m3r @ Jan 24 2008, 03:01 PM) *
I check the IP address that was logged in with, with the one trying to get the request.

That would work with Users that have a Static IP, but how do you handle Dynamic IP's?

Reply

FLaKes
Nice one, I had to added it my favorites to consider it for whenever I am programming something with php. I am currently working on an admin cp for a business that sells boots. This will really come in handy. Thanks.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : discovered, sessions, php,

  1. Php Sessions And Post Variables Issues
    My script dosent seem to work as intended (1)
  2. <?php ?> Unique Visitors Script
    Flat file unique visitors script (no sessions) (2)
    This is really simple script. Well at least this part is, but it could be extendable. Only problem
    is that it's not really for massive websites with hundread of visitors a day, but rather for
    small ones. But it is a good script to figure out how to make a visitor counter script. Anyway
    here's the snippet. CODE <?php function getVisits($variable) {
        $visits = array();     if ($handle =
    opendir('stats/')) {     while (false !== ($file =
    readdir($handle))) {  ....
  3. Using Sessions Instead Of Cookies, Help Please
    (1)
    This is a simple code to register and login.. this uses cookies.. i want to use sessions instead..
    can someone tell how i can do it ? config.php CODE <?    ob_start(); // allows you
    to use cookies    $conn =
    mysql_connect("localhost","USER","PASSWORD");   
    mysql_select_db(DATEBASE) or die(mysql_error());    //fill in the above
    lines where there are capital letters.    $logged = MYSQL_QUERY("SELECT * from users
    WHERE id='$_COOKIE[id]' AND password = '$....
  4. Sessions And Login
    Without Cookies (5)
    Hi, I have a login script i made using PHP sessions and MySQL. It works fine but there is a
    problem. As you know Sessions are stored in Cookies by PHP. So if someone has switched Cookies off
    then no sessions will work. How to solve this problem ? Please help me. Thanks and have a good
    day. ....
  5. Session Variables
    Sessions in PHP behaving strangely (4)
    Hi. I am part of a development team working in PHP and MySQL. The site is using SSL, and users
    have to log to use the site. When users log in, their important details are retrieved from the
    database and stored in session variables (functionality in the site is permissions specific).
    Lately, sessions are "disappearing" for no apparent reason. Users will log in, and at some point
    (the length of time will vary unpredicatably) the sessions will lose their value (the variables are
    empty) and this causes the site to evict the user. This is very frustrating for the user b....
  6. Php Sessions
    Multiple users using the same login (2)
    Hi, I'm realtively new to PHP and I'm considering creating some login functionality.
    However I want a group of users to use the same loginname and password. They will be loggin infrom
    different machines. The users will know they are sharing the account. Can anyone give me an idea
    of what kind of effect this might have on my sessions? Will it create any odd hiccups or other
    strange things?....
  7. Sessions
    heeeelllllp!!!!!! (8)
    Hi. Im making a flatfile chatroom. Ive been making it about a month because I dont understand the
    sessions. When the user enters the room he/she has to enter a nickname. But when the user enters a
    message the chatroom "forgets" the nickname. Ive been messing around with sessions but I cant get em
    to work. Session_start(); thingy is in the right place (at the top). Ive looked through about 10
    diferent tutorials but they were useless. How do I make it to pass the nickname variable to multiple
    pages? And please dont tell me to search google or use php.net because ive alread....

    1. Looking for discovered, sessions, php,

Searching Video's for discovered, sessions, php,
advertisement



Something I Discovered With Sessions [php]



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE