0

This one may take a little but please stay with me for it.

I have been working with this code:

if(isset($_GET['o']) && isset($_GET['q'])){
    echo("WIN 1");
    if($_GET['o'] == "remove") {
        echo("WIN 2");
        $_SESSION['product_'.$_GET['q']]-=1;
        echo("WIN 3");
        if($_SESSION['product_'.$_GET['q']] < 1) {
            unset($_SESSION['product_'.$_GET['q']]);
            echo("WIN 4");
            redirect("checkout.php");
        } else {
            echo("LOSE");
            redirect("checkout.php");
        }
    }
}

And it is not performing any of the functions unless I step through it using:

exit();

After each of the echos. I have no idea what could be causing this issue.

The URL being passed is:

localhost/cart.php?q=1&o=remove

Thank you!

EDIT

This is how I set the session (I have removed the echos):

if(isset($_GET['q'])) {
        $query = query("SELECT * FROM products WHERE product_id=".escape_string($_GET['q'])."");
        confirm($query);
        while($row = fetch_array($query)){
            if($row['product_quantity'] != $_SESSION['product_'.$_GET['q']]){
                $_SESSION['product_'.$_GET['q']]+=1;
                redirect("checkout.php");
            } else if($row['product_quantity_tracking'] == "No") {  //CHECK TO SEE IF IT'S INVENTORY TRACKED
                redirect("checkout.php");
            } else {
                set_message("Sorry! There isn't enough in stock to complete that request!");
                redirect("checkout.php");
            }
        }
    }

This is the redirect function:

function redirect($location){
    header("Location: $location");
}

I hope this helps!

CBNP
  • 3
  • 3
  • i think session product is empty – Jerson Sep 30 '20 at 07:33
  • What seems to be the problem and what is your issue. please provide some details about your issue or error as it's not clear in your question. – Tufail Ahmad Sep 30 '20 at 07:34
  • the problem is -= 1 , session is not initialized the product_ + id , your substracting NULL - 1 , It will throw an error, undefined index – Jerson Sep 30 '20 at 07:37
  • @Jerson, Session is set fine, you cannot get to this page without having the session variable set. – CBNP Sep 30 '20 at 11:25
  • @TufailAhmad I would gladly provide more detail but there are no more details that I can get from the problem, this is why I am having to ask on here :( – CBNP Sep 30 '20 at 11:25
  • What is the value of $_SESSION['product_1']. before executing this code? You can echo it and check the value whether it's integer or not – Tufail Ahmad Sep 30 '20 at 11:35
  • @TufailAhmad the value of $_SESSION['product_1'] is indeed an INT as it is parsed through intval before it is handed to the session details. – CBNP Sep 30 '20 at 11:43
  • Did you use session_start(); somewhere? – Martin Oct 02 '20 at 08:27
  • @Martin yes I have been. All my other session variables work. I can add to the $_SESSION['product_1'] count, it just won't remove anything from it. – CBNP Oct 02 '20 at 10:01

2 Answers2

0

By using exit(); you stop the execution of your script and you can see everything that was printed out using your echos.

-> If you can the see something like 'WIN 1', 'WIN 2', ... when exiting your script, it is working fine until this line.

-> Your problem must be found after this line.

I guess your problem lies in the redirect() function. See this for how to perform a redirect: How do I make a redirect in PHP?

Martin
  • 621
  • 7
  • 16
  • I have the `redirect()` code as a function using headers in a seperate file. And that isn't an issue. What is an issue is if I step through the function it works fine. But if I let it run through as intended, it doesn't work – CBNP Sep 30 '20 at 11:19
  • Ok, can you please show how you set the session and the redirect function? – Martin Oct 01 '20 at 12:41
  • I have updated the question with the redirect and session functions – CBNP Oct 02 '20 at 00:14
0

So, for some reason, when I hit the redirect on the cart.php it was running the REST OF THE PAGE instead of just redirecting and exiting the page as it would be expected to do.

For me to have solved this issue, I had to change the code to the following:

if(isset($_GET['o']) && isset($_GET['q'])){
    if($_GET['o'] == "remove") {
        $_SESSION['product_'.$_GET['q']]-=1;
        if($_SESSION['product_'.$_GET['q']] < 1) {
            unset($_SESSION['product_'.$_GET['q']]);
            redirect("checkout.php");
            exit();
        } else {
            redirect("checkout.php");
            exit();
        }
    }
}

By adding the exit(); after the redirects it made sure it didn't follow the rest of the script on the page.

Thank you for everyone's help!

CBNP
  • 3
  • 3