#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Question Overwrite PHP SESSION [help]


    Hey guys, I'm new to the forums, need a little bit of PHP help. I have a website setup with wizard alike tool which runs through a session, when the user inputs the information, it goes to the next page, they then select the product and then it goes to the cart on which when they checkout we know what the information the user has inputted. Here is some photos to demonstrate how it works (sorry about the URLs):-

    Page 1 (wizard.php):
    i50.tinypic.com/2d19weu.png

    Page 2 (wizard2.php):
    i50.tinypic.com/2cyidkm.png

    Page 3 (checkout.php):
    i49.tinypic.com/20ppulg.png

    Page 4 (checkout-shipping.php):
    i46.tinypic.com/30axurs.png

    - Then the next few pages include the payment process, etc. Then we receive the order and open the invoice and can see the customer wizard information and know which colour they would like.

    Works 100% fine, problem is, when a customer uses the wizard twice, it overwrites the session information. e.g: if i use the wizard and pick colour blue, then head to the checkout, all the information displays perfecty! - Although, if i need the same product again in orange, i will re-use the wizard again but choose 'orange' but when i go to the checkout, my blue product information has gone and has been replaced with the orange information. It's showing two products in the carts quantity, just not the information twice, its overwriting the information. Basically, i require it to input the wizard data twice, instead of overwriting everytime someone uses the wizard for the same products. If i have 5 products i need all the same but different colours, it would only enter the wizard information once, but i need all 5 information in the comments box.

    Hope this makes sense. I can provide the URL if you PM me on the forums, could anyone help me out with this?

    Many Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,622
    Rep Power
    595
    Based on what you posted, what exactly are you expecting us to tell you? Please see ManicDan's New User Guide.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by gw1500se
    Based on what you posted, what exactly are you expecting us to tell you? Please see ManicDan's.
    As said, needing some help with PHP sessions? Quite a useful guide, but doesn't mention much on PHP sessions except refer to the PHP website to view the manuals, which i have already done that and cannot figure it out. I need help from someone who understands sessions more than i do for a quick fix. Haven't really got time to be reading all the manuals on sessions, not that i wouldn't want to learn more of it, its just that this needs to be fixed quickly, so needing help on this. Anyone have a fix for the session to not overwrite the information?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,622
    Rep Power
    595
    OK, based on what information you gave us, I can answer this way. Where you deal with your session variables, test to see if it is already defined. If so add to it rather than overwrite it. Also be sure not to do a destroy until you are sure the session is done.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    What you need to do is restructure the session data so that it's stored as an array instead of a single value.

    For example, if currently you have:
    PHP Code:
    $_SESSION['product'] = array(
      
    'color' => 'blue',
      
    'size' => 'Large',
    ); 
    You would instead have to do:
    PHP Code:
    $_SESSION['product'][] = array(
      
    'color' => 'blue',
      
    'size' => 'Large',
    ); 
    You will also need to update the display code for $_SESSION['product'] and add a loop, since it is now an array instead of a string.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Still struggling with this and can't figure it out.

    Originally Posted by gw1500se
    Also be sure not to do a destroy until you are sure the session is done.
    Checked all files and session_destroy () is only on the last page of the checkout process. Once the payment has been sent. Session destroy is also active when they use the logout.php, all the other pages do not have it, so not sure whats going on.
    Originally Posted by E-Oreo
    What you need to do is restructure the session data so that it's stored as an array instead of a single value.

    For example, if currently you have:
    PHP Code:
    $_SESSION['product'] = array(
      
    'color' => 'blue',
      
    'size' => 'Large',
    ); 
    You would instead have to do:
    PHP Code:
    $_SESSION['product'][] = array(
      
    'color' => 'blue',
      
    'size' => 'Large',
    ); 
    You will also need to update the display code for $_SESSION['product'] and add a loop, since it is now an array instead of a string.
    Thank you very much for this. We are using something a little different below. I'm not very PHP familiar, but i made this myself and does seem to work perfectly fine, its just overwriting the information when a customer does try to use the wizard twice. We are using osCommerce BTW.

    wizard.php: (i50.tinypic.com/2d19weu.png)
    PHP Code:
    // redirect the customer to a friendly cookie-must-be-enabled page if cookies are disabled (or the session has not started)
      
    if ($session_started == false) {
        
    tep_redirect(tep_href_link(FILENAME_COOKIE_USAGE));
      }

      require(
    DIR_WS_LANGUAGES $language '/' FILENAME_LOGIN);

     
      
    $breadcrumb->add('Wizard'tep_href_link('car-wizard.php''''SSL')); 
    wizard2.php: (i50.tinypic.com/2cyidkm.png)
    PHP Code:
    // redirect the customer to a friendly cookie-must-be-enabled page if cookies are disabled (or the session has not started)
      
    if ($session_started == false) {
        
    tep_redirect(tep_href_link(FILENAME_COOKIE_USAGE));
      }

      require(
    DIR_WS_LANGUAGES $language '/' FILENAME_LOGIN);
      
      
    $comments '';
      
      if (
    $_POST['colour-code']) $comments .= 'Colour Code: '.$_POST['colour-code']."\n"
      if (
    $_POST['base-colour']) $comments .= 'Base Color: '.$_POST['base-colour']."\n"
      if (
    $_POST['original-colour']) $comments .= 'Original Colour: '.($_POST['original-colour']=='1'?'Yes':'').($_POST['original-colour']=='2'?'No':'')."\n"
      if (
    $_POST['make']) $comments .= 'Make: '.$_POST['make']."\n"
      if (
    $_POST['custom-make']) $comments .= 'Make: '.$_POST['custom-make']."\n"
      if (
    $_POST['custom-model']) $comments .= 'Model: '.$_POST['custom-model']."\n"
      if (
    $_POST['year']) $comments .= 'Year: '.$_POST['year']."\n"
      
      if (
    $commentstep_session_register('comments');
      
      if (!
    $commentstep_redirect('car-wizard.php');
     
      
    $breadcrumb->add('Wizard'tep_href_link('car-wizard.php''''SSL')); 
    checkout.php: (i49.tinypic.com/20ppulg.png)
    PHP Code:
      if ($cart->count_contents() > 0) {
        include(
    DIR_WS_CLASSES 'payment.php');
        
    $payment_modules = new payment;
      }
      
      
    $comments '';

      if (
    $commentstep_session_register('comments');

      
    tep_session_unregister('comments');

      require(
    DIR_WS_LANGUAGES $language '/' FILENAME_SHOPPING_CART);

      
    $breadcrumb->add(NAVBAR_TITLEtep_href_link(FILENAME_SHOPPING_CART)); 
    checkout-shipping.php: (i46.tinypic.com/30axurs.png)
    PHP Code:
      if (is_array($_SESSION['comments2'])) foreach ($_SESSION['comments2'] as $key => $val) {
         
    $comm .= "Products ID: ".$key."\n\n".$val."\n\n";
      }

    if (!
    tep_session_is_registered('comments')) tep_session_register('comments');
    if (
    tep_not_null($HTTP_POST_VARS['comments'])) {
    $comments tep_db_prepare_input($HTTP_POST_VARS['comments']);
    } else { 
    $comments ''; }
      
      
    // $comm = str_replace('Array','',$comments2);
      
      
    $_SESSION['comments'] = $comm
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,622
    Rep Power
    595
    You need to keep in mind that PHP is stateless. That means once you have output the page, all information is gone as far as PHP is concerned. It knows nothing about what has happened previously. That is why you need to use a session.

    That being said, I see you use a variable called '$session_started'. However, I do not see where that is ever set to true. But even it if was, when you test it, keep in mind the above. Also, since I don't see a session_start anywhere (there needs to be one in every requisite script), you are not really using a session at all.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo