+ Reply to Thread
Results 1 to 4 of 4

Thread: session_register vs $_SESSION - PHP

  1. #1
    freddye is offline x10Hosting Member freddye is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    7

    Post session_register vs $_SESSION - PHP

    Hello,

    I've been working on a Login for my site.

    I got some code on it from some site which uses session register. Later I read that session_register is 'depreciated' (does that mean it's not in use?) and that I should use $_SESSION.

    My question is what would the equivalent code be for registering the session. Also to be able to use $_SESSION I need to start the session but that needs to go on the very top of my code... but I don't want to start the session until I authenticate the user.

    I may have all of this confused (just started learning PHP)

    The code I have currently:

    <?php
    include('connectionfile.php');
    $tbl_name="listserve";
    $username=$_POST['username'];
    $password=$_POST['password'];

    // To protect MySQL injection (more detail about MySQL injection)
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

    $sql="SELECT * FROM $tbl_name WHERE email='$username' and password='$password'";
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $username and $password, table row must be 1 row

    if($count==1){
    // Register $username, $password and redirect to file "login_success.php"
    session_register("username");
    session_register("password");
    header("location:loginsuccess.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>

    Thanks,
    FreddyE

  2. #2
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: session_register vs $_SESSION - PHP

    'depreciated' does mean that it has become obsolete.

    $_SESSION is the current format, in much the same way the $HTTP_POST_VARS has become $_POST.

    The Session start command is


    PHP Code:
    session_start(); 
    and must always (as you say) come at the very start of the page.

    Don't worry about starting the session before authentication (I have this system on my site).

    All that the session does is store data in server memory and has nothing to do with logins directly - only what you tell it!

    For instance, if you start the session and do this...

    PHP Code:
    $_SESSION['something'] = "boo"
    The only thing stored to session memory is "boo".

    You will note that in your code, you have an (if) statement, asking the database if there is a row that matches the posted data.

    If this is true, then the data is stored to session memory.

    The code should read

    PHP Code:
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password
    I must say though that this method is very old and subject to session attacks, using JS URL entries. Passwords should never be stored in memory, especially if they are not encrypted.

    Hope this helps a bit

  3. #3
    freddye is offline x10Hosting Member freddye is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    7

    Post Re: session_register vs $_SESSION - PHP

    Wow thanks Rich for your help.

    I was searching the web for a bit and couldn't get a direct answer when I decided to do my first post on the forum. Thanks again.
    Edit:
    I'm new to the forum and don't know much about credits. What is the common donation for an answered question??
    Last edited by freddye; 04-20-2009 at 01:09 PM. Reason: Automerged Doublepost

  4. #4
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: session_register vs $_SESSION - PHP

    Glad I could help.

    Donations very much depend on personal preference, but for my part, I'm not concerned with increasing credits.

    You can also add to someone's reputation (if you like) by clicking the add to reputation button.

    Good luck!

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. PHP Easter Eggs
    By dragoneye_xp in forum Off Topic
    Replies: 3
    Last Post: 06-14-2006, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers