views:

329

answers:

5

I have a PHP page and I want to share some data between pages like UserID, password.

I'm learning about sessions and I'm not sure if Im using it correctly.

<?php
require_once('database.inc');
$kUserID = $_POST['kUserID'];
$kPassword = $_POST['kPassword'];

if (!isset($kUserID) || !isset($kPassword)) { 
    header( "Location: http://domain/index.html" ); 
}

elseif (empty($kUserID) || empty($kPassword)) { 
    header( "Location: http://domain/index.html" ); 
} 
else { 
    $user = addslashes($_POST['kUserID']); 
    $pass = md5($_POST['kPassword']); 
    $db = mysql_connect("$sHostname:$sPort", $sUsername, $sPassword) or die(mysql_error()); 
    mysql_select_db($sDatabase) or die ("Couldn't select the database."); 
    $sqlQuery = "select * from allowedUsers where UserID='" . $kUserID . "' AND passwordID='" . $kPassword . "'";
    $result=mysql_query($sqlQuery, $db);
    $rowCheck = mysql_num_rows($result); 
    if($rowCheck > 0){ 
        while($row = mysql_fetch_array($result)){
            session_start();
            session_register('kUserID'); 
            header( "Location: link.php" );
       } 
    } 
    else { 
        echo 'Incorrect login name or password. Please try again.'; 
    } 
} 
?> 
+4  A: 

Be aware that session_register() is deprecated in favor of assigning values to the $_SESSION superglobal, e.g.

<?php
    $_SESSION['hashedValue']= '437b930db84b8079c2dd804a71936b5f';
?>

Also be aware that anything stored in a session, especially in a shared-server environment, is fair game. Never store a password, regardless of whether it's hashed or encrypted. I would avoid storing a username as well. If you must use some authentication mechanism between pages using a session variable, I'd recommend using a second lookup table, e.g. logins, and store the username, login time, etc in that table. A hashed value from that table is stored in the session, and each page request checks the time in the table and the hashed value against the database. If the request is either too old or the hash doesn't match, force re-login.

All this and more is available to you in the PHP manual section on sessions.

bdl
+1  A: 

First, you need to put session_start() at the very beginning of your script. It also needs to go at the start of every script that uses session data. So it would also go at the top of babyRegistration.php.

Second, I would strongly recommend against using session_register() as it relies on register_globals which is off by default for security reasons. You can read more here: http://php.net/manual/en/security.globals.php. You can add/access session variables by using the $_SESSION superglobal:

$_SESSION['kUserID'] = $kUserID;

Last, not really session related, just an observation, your isset check at the top is redundant; empty will return true for an unset/NULL variable, just as you might expect.

emmychan
+12  A: 

For the love of all that is holy, don't use addslashes to prevent SQL injection.

I just owned your site:

Image of your ownt site

Edit: Even worse.

I just noticed that you're attempt at preventing injection via addslashes, isn't even being used!

<?php
$kUserID = $_POST['kUserID'];
$user = addslashes($_POST['kUserID']); // this isn't used
$sqlQuery = "select * from allowedUsers where UserID='"
  . $kUserID . "' AND passwordID='" . $kPassword . "'";
hobodave
So, would adding the data into the database using a SQL Injection attack and then retrieving it on another page via the same mechanism count as a viable way to save data between pages? Yikes. Good thing that code was posted here early.
TheJacobTaylor
here is a link to the function you should use instead of addslasheshttp://php.net/manual/en/function.mysql-real-escape-string.php
Jayrox
A: 

At the top of a page

session_start();
$_SESSION['yourvarname']='some value';

then on some other page to retrieve

echo $_SESSION['yourvarname'];
// some value

Oh and about injection,use this on everything going into your db http://us3.php.net/manual/en/function.mysql-real-escape-string.php

David Morrow
@dperry> Good example, but no such function as sessionStart().
bdl
please stop advocating the use of the old mysql functions. teach new people the proper way to do things with PDO http://php.net/pdo
Kris
i personally use mysqlli http://us.php.net/manual/en/class.mysqli.php so in that case it would be $mysql_li_instance->real_escape_string( $value ); same concept though of passing all incoming strings through the real_escape_string filter
David Morrow
+2  A: 

You might also wanna rename "database.inc" to "database.inc.php", or properly setup your host to treat ".inc" as PHP:

http://www.namemybabyboy.com/database.inc

<?php
    $sDatabase = 'shayaanpsp';
    $sHostname = 'mysql5.brinkster.com';
    $sPort     = 3306;
    $sUsername = 'shayaanpsp';
    $sPassword = 'XXXX';
    $sTable    = 'allowedUsers';
?>
truppo
LOL. His password is in a couple of my password dictionaries too.
hobodave