Using Sessions!

Posted on 2004-11-04
Last Modified: 2006-11-17
Ok, this is my first project in which i use sessions!

My impression from my reading is i just need to use this 'session_start();' i have it placed in the first line on my pages.    This works fine for me on my first page. then when i go to the second page it only half works.. here is my code for the second page..

require 'function.php';

$sql = "select * from vin where vin='".$_SESSION[vin]."'";
$detail_report ='<br>Sql Statement run :' .$sql;
$result = mysql_query($sql,$conn) or die(mysql_error());
$vin = mysql_fetch_array($result);
                  echo '<br> '.$vin['vin'];
                  echo '<br> '.$vin['make'];
                  echo '<br> '.$vin['model'];
                  echo '<br> '.$vin['year'];

$sql = "select * from customer where customer_id='".$vin['currentownerID']."'";
$detail_report ='<br>Sql Statement run :' .$sql;
$result = mysql_query($sql,$conn) or die(mysql_error());
$customer = mysql_fetch_array($result);
                  echo '<br> '.$customer['firstname'].' '.$customer['lastname'];
                  echo '<br> '.$customer['address'];
                  echo '<br> '.$customer['city'].', '.$customer['state'].' '.$customer['zip'];
                  echo '<br> '.$customer['phone'];
                  echo '<br> '.$customer['phone2'];
                  echo '<br> '.$customer['notes'];

$sql = "select * from services";
$result = mysql_query($sql,$conn) or die(mysql_error());
$services = mysql_fetch_array($result);
$servicesrows = mysql_num_rows($result);

echo '<br><br><br>number of rows in the serivce table'.$servicesrows;

while ($catagoryArray = mysql_fetch_array($result)) {
                        // give a name to the fields
                        echo '<br>'. $catagoryArray['name'];



Ok now the first time this page is run it works if it is refreshed at all or anything the session value is empty so it just pulls empty queries from the databse!? i know the session is still active cause when i go back to the previous page my session still works there.

Is there another command or something that i am missing?
Question by:mightofnight
    LVL 2

    Expert Comment

    I think you forgot to put " or ' in $_SESSION[vin]. so it will be $_SESSION['vin'] or $_SESSION["vin"].
    LVL 2

    Expert Comment

    It is good programming practice to include the index as a string as TheUnkind mentioned.  Read for more details.

    That being said, it is probably the case that php is not quoting the vin for you.

    Here's a good tip for ya. . .use print_r.

    If you are unsure of what's in the session array. . .

    echo "<pre>";
    print_r( $_SESSION );
    echo "</pre>";

    LVL 2

    Author Comment

    Hum Thanks guys ireally appreciate the print_r statement whats the <pre></pre> i am not famiular with that tag? Also what does the prupose of the exit(); serve?

    The problem must have been with my code cause after i posted i just passed on the variable using get so that i didnt' have to stop.  Now i changed it back to the $_SESSION['vin'] and it works.. it still worked with out the '.  the first examples i learned on never used thoes in array's so i have been tryign to change my habbits :P.

    also how do you get the session id? i was going to base a statement on the vin but just realized when i went to write the code that i really needed the session id?
    LVL 2

    Accepted Solution

    The pre tags are html.  . . it just helps when reading the debug information from the print_r.  exit is the same as die().  It stops the script from executing after that point.  I use those 4 lines for debugging purposes.  It is not necessary to include the pre's or the exit.

    there is a function session_id().  Also there is a constant defined as SID.  I suggest reading the php manual on sessions.  This will give you a good handle on sessions and might save ya a few headaches along the way. . .

    LVL 2

    Author Comment

    Thanks d_tan!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now