Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2003, 03:38 AM
Authorized User
 
Join Date: Sep 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to major dynamic123
Default need help on sessions.

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

 
Old November 7th, 2003, 11:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

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

:D



the genuine genius
 
Old November 7th, 2003, 02:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old November 7th, 2003, 02:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And use $_GET instead of $HTTP_GET_VARS. =)

Take care,

Nik
http://www.bigaction.org/
 
Old November 7th, 2003, 02:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old November 10th, 2003, 05:02 AM
Authorized User
 
Join Date: Sep 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to major dynamic123
Default

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.

 
Old November 10th, 2003, 02:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old November 11th, 2003, 03:31 AM
Authorized User
 
Join Date: Sep 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to major dynamic123
Default

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

 
Old November 11th, 2003, 04:51 AM
Authorized User
 
Join Date: Sep 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to major dynamic123
Default

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


 
Old November 11th, 2003, 05:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/





Similar Threads
Thread Thread Starter Forum Replies Last Post
sessions p2ptolu Classic ASP Components 3 March 17th, 2005 06:31 AM
Sessions 2540EA BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 February 20th, 2005 05:42 AM
Mixing classic ASP sessions with ASP.NET sessions scorpion_king General .NET 2 August 4th, 2004 08:20 AM
Sessions aeejai Beginning PHP 3 April 22nd, 2004 01:51 PM
Help sessions ittorget Pro VB 6 2 April 20th, 2004 02:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.