Subject: need help on sessions.
Posted By: major dynamic123 Post Date: 11/7/2003 2:38:14 AM
Hi. I am developing a shopping cart using php scripts and sessions. I have just created a simple script. Here's the script.


Filename: showitem.php
<?php
   session_start();

   echo 'book 1';
   echo '<br>';
   echo 'RM 123.12';
   echo '<br>';
   echo '<a href="showcart.php?id=123&price=123.12&basket=War against iraq:bush and saddam">Add to cart</a>';

   echo '<p>';   
   echo 'book 2';
   echo '<br>';
   echo 'RM 456.45';
   echo '<br>';
   echo '<a href="showcart.php?id=456&price=456.45&basket=item 2 of 2">Add to cart</a>';

   echo '<p>';   
   echo 'php programming';
   echo '<br>';
   echo 'RM 789.89';
   echo '<br>';
   echo '<a href="showcart.php?id=php programming&price=789.89&basket=php programming">Add to cart</a>';
?>



Filename: showcart.php
<html>
<head><title>dasda</title></head>
<body>


<?php
   session_start();

   $id=$HTTP_GET_VARS['id'];
   $price=$HTTP_GET_VARS['price'];
   $basket=$HTTP_GET_VARS['basket'];
  
   if (session_is_registered("ses_basket_items")) {
      // add the next item here
      // whenever I click the next book to be inserted into
      // the shopping cart, it doesn't come in here.
      echo 'session already exist';
   }
   else {   
         $ses_basket_items=$ses_basket_items+1;
         $ses_basket_id[0]=$id;
         $ses_basket_price[0]=$price;
         $ses_basket_name[0]=$basket;

         session_register("ses_basket_items");
         session_register("ses_basket_id");
         session_register("ses_basket_price");
         session_register("ses_basket_name");
   }

   
   // okay. no problem.
   if ($ses_basket_items>0) {
      echo '<p>';
      echo 'basket contains<br>';
      for ($counter=0; $counter<$ses_basket_items; $counter++) {
         $item_id=$ses_basket_id[$counter];
         $item_name=$ses_basket_name[$counter];
         $item_price=$ses_basket_price[$counter];
         
         echo $ses_basket_items;
         echo '<br>';
         echo $item_id;
         echo $item_name;
         echo $item_price;
         echo '<br>';
       }
   }

?>


<a href="javascript:history.back()">Back to books</a>
</body>
</html>





I have some problems with my program. I am trying to develop a shopping cart using sessions. When I click "add" in the showitem.php page, it successfully adds book 1 to the cart. But when I click "add" on the next item(eg: book2), it doesn't go to the if statement:
if (session_is_registered("ses_basket_items"))

Instead, it goes back to the else statement and overwrites the first array item, book 1.

I hope someone can help me correct my program code. I have tried reading my programming code again and again. I can't see the mistake I've made.

Hope someone in this forum can help me out.

Best regards,
Major

Reply By: Moharo Reply Date: 11/7/2003 10:19:08 AM
depending on your php version, i would recommend using $_SESSION[] array for storing session variables... check this topic...

http://p2p.wrox.com/topic.asp?TOPIC_ID=2052





the genuine genius
Reply By: nikolai Reply Date: 11/7/2003 1:42:00 PM
Before I get into it, you must make sure you're calling session_start() BEFORE any output is sent to the browser.  This includes all whitespace and text before your PHP open tag (<?php).  Your posted code has the <html><head>... tags before you call session_start().


Take care,

Nik
http://www.bigaction.org/
Reply By: nikolai Reply Date: 11/7/2003 1:42:43 PM
And use $_GET instead of $HTTP_GET_VARS.  =)

Take care,

Nik
http://www.bigaction.org/
Reply By: nikolai Reply Date: 11/7/2003 1:56:53 PM
Okay, reading through your code, I don't see anywhere where you add anything else to your cart.  Also, it doesn't make sense to store a counter in your session when the counter just holds the size of an array ALSO in the session.  Why not just calculate the size of the array?


Here's a simple rewrite of showcart.php:
<?php
    session_start();

    $id    = $_GET['id'];
    $price = $_GET['price'];
    $name  = $_GET['basket']; // why is this named "basket" instead of "name"??
   
    if (! isset($_SESSION['basket']))
    {
        $_SESSION['basket'] = array();
    }
   
    $_SESSION['basket'][] = array('id'    => $id,
                                  'price' => $price,
                                  'name'  => $name);
   
    // okay. no problem.
    if (!empty($_SESSION['basket']))
    {
        echo "<p>\n";
        echo "basket contains<br />\n";
        foreach($_SESSION['basket'] as $basket_item)
        {
            echo $basket_item['id']    . ' '
               . $basket_item['name']  . ' '
               . $basket_item['price'] . "\n";
            echo '<br>';
        }
    }

?>
<a href="javascript:history.back()">Back to books</a>
</body>
</html>



It still needs to be cleaned up (include all the proper missing HTML tags like <html><head><body> etc...), but that's an easy exersize I leave to you.



Take care,

Nik
http://www.bigaction.org/
Reply By: major dynamic123 Reply Date: 11/10/2003 4:02:27 AM
Hi. I've tried to use your code. I still cannot see a list of item being printed out at the showcart.php page. I think the latest item selected from the showlist.php is still overwriting the array at showcart.php. I cannot print a list of items clicked at the showitem.php page.

Reply By: nikolai Reply Date: 11/10/2003 1:59:32 PM
Really?  Works for me.

The latest item selected from showlist is *not* overwriting the session array that's accessed in showcart.php.  I can guarantee you that because we APPEND to the basket array using the empty-bracket notation:
  $_SESSION['basket'][] = array(...);

This creates a new index in $_SESSION['basket'].  The value stored at that index is the array that holds the id, name, and price of the book selected.

If you're only seeing the last item selected, then I suspect there's something screwy with your PHP configuration.  Make sure your session save handler is "files" (since you don't define your own session handler functions), your session storage directory exists, and your webserver/PHP has read/write permissions in that directory.


Take care,

Nik
http://www.bigaction.org/
Reply By: major dynamic123 Reply Date: 11/11/2003 2:31:09 AM
Hi. Thanks for the reply. Frankly speaking, I cannot understand what you were trying to explain about the PHP compiler. Can you provide me the full path to the specific configuration file? I am new to PHP. I hope you will guide me through this procedure.

Once again, thank you for your solution.

Regards,
Major

Reply By: major dynamic123 Reply Date: 11/11/2003 3:51:59 AM
Hi. I have tried to troubleshoot the problem myself. I have checked the php.ini-dist file.

...
[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler.  In the case of files, this is the path
...

This is what I see on the file. Seems okay to me. What else did I not setup?

Regards,
Major


Reply By: nikolai Reply Date: 11/11/2003 4:25:03 AM
The config file is php.ini.  php.ini-dist is a sample config file that's distributed with PHP.  It has all the default settings set, which makes configuration easy since you only need to change a couple things (if any) to get PHP up and running.

The session handler should be files, so that looks good, but there's also a session.save_path where PHP will store your session data.  It's this directory that needs to exist with read/write permissions for the web user.



Take care,

Nik
http://www.bigaction.org/
Reply By: major dynamic123 Reply Date: 11/12/2003 11:28:18 PM
Hi. Thanks for your reply. I have extracted a portion of the php.ini file on the session variable. I do not know what to change on the "session.save_path = /tmp"
variable. I need some help here.

...
; Argument passed to save_handler.  In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
; As of PHP 4.0.1, you can define the path as:
;     session.save_path = "N;/path"
; where N is an integer.  Instead of storing all the session files in
; /path, what this will do is use subdirectories N-levels deep, and
; store the session data in those directories.  This is useful if you
; or your OS have problems with lots of files in one directory, and is
; a more efficient layout for servers that handle lots of sessions.
; NOTE 1: PHP will not create this directory structure automatically.
;         You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
;         use subdirectories for session storage
session.save_path = /tmp

; Whether to use cookies.
session.use_cookies = 1
...

Reply By: nikolai Reply Date: 11/13/2003 9:34:05 PM
Okay, do you have a windows machine, or a unix/linux machine?  If windows, then you'll use a windows path (e.g. c:\temp).  If linux, you'll use a unix-style path (e.g. /tmp).

This directory is where PHP will store all the active session data, so you need to make sure the directory you specify exists and that PHP has access to read and write files in that directory.


Take care,

Nik
http://www.bigaction.org/
Reply By: major dynamic123 Reply Date: 11/14/2003 8:35:22 PM
Hi, Nikolai. Thanks for your help. I've got the PHP script running. By the way, I have some questions about the code you've published here.

 $_SESSION['basket'][] = array('id'    => $id,
                                  'price' => $price,
                                  'name'  => $name);

What does the "=>" symbol mean?

Regards,
Major

Reply By: nikolai Reply Date: 11/14/2003 8:55:08 PM
=> is an operator that establishes the association between an array key (a.k.a. index) and a value.

For more info:
  http://www.php.net/types.array
  http://www.php.net/function.array


You also see this operator in the foreach() construct:

foreach ($my_array as $key => $value)
{
   ...
}


Take care,

Nik
http://www.bigaction.org/

Go to topic 6508

Return to index page 1005
Return to index page 1004
Return to index page 1003
Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997
Return to index page 996