0

I'm currently making a session shopping cart where I can add and delete products with a link underneath every product. You can check it out here if you prefer it visualized: http://www.bgc-testomgeving.nl/sem

Anyway, somehow when I click the link of the product nothing seems to happen except from the URL changing, but the adding to the $_SESSION['cart'] doesn't seem to occur.

This is the code of the link underneath every product to add it to the cart: echo '<a href="action=add&id=' .$id. '">Voeg toe </a>';

With $id being: $id = get_the_ID(); from WordPress query. This part works however because the URL of the <a> is right I think.

Here I check if there is already a $_SESSION['cart'] active, if not I make one. Also, I check if the item ID is already in cart, if not, I do the array_push() and if so, I use unset. See underneath:

<?php session_start(); 
require("dbconnect.php");
?>
<?php 
     if(!isset($_SESSION['cart'])) {
         $cart = array();
         $_SESSION['cart'] = $cart;
     }  

    if(isset($_GET['action']) && $_GET['action']=="add"){             
        $id=intval($_GET['id']); 
                if(in_array($id, $_SESSION['cart'])){
                    if (($key = array_search($id, $_SESSION['cart'] !== false))){
                        unset($_SESSION['cart'][$key]);
                    }
                }
                else {
        array_push($_SESSION['cart'],$id);          
                }
    } ?> 

I print the array of the $_SESSION['cart'] plus the session_id() on the page to check if it worked with the following code in index.php:

<?php print "Test session number: "; 
            echo session_id();
            echo '<br>';
            echo serialize($_SESSION['cart']);
            ?>

So my question is, why is the $id not pushed to the $_SESSION['cart']

EDIT: I FOUND THE PROBLEM, I FORGOT TO ADD A ? TO THE ARRAY SO THE $_GET didn't know it was a parameter

1
  • Okay, so locally this works perfectly fine. What server settings could be the problem? I actually have no idea, only thing I know is that register_globals is disabled, could that be the problem? Commented Mar 1, 2016 at 13:25

2 Answers 2

3

Your Script is working. I copy your code in a PHP-File on my server and it is doing what you expect. I can add IDs to my "cart". Is your session working correctly? Some wrong configs on your Server?

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for trying, I was setting up my own server already to try if that was the problem. Is register globals supposed to be enabled? Maybe that's the problem?
No, register globals is per default deactivated. Is your browser maybe rejecting your Cookies(private mode or something), so the Session is always restarting and always empty?
Hey @Candyman1332 the problem is solved, I forgot a question-mark in front of my action so <a href="?action.....> fixed the problem. Took me 4 hours of searching.
thx for the responds. I saw it at the moment when i sent the comment. :D
0

var_dump the $_SESSION['cart'] before and after the array_push($_SESSION['cart'],$id); and check the values.

1 Comment

I'm settings up my local server right now and will do this ASAP and post the results here. My hosting company won't allow me to change my php.ini file so I can't debug right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.