PHP ignoring cookie

697

I'm making a mysqli query with limits that are set via $_COOKIE.

The way it works is that I send the query with the URL and PHP turns that into a cookie so the user does not see any change in their URL.

Then PHP reads the cookie and makes the query, and then clears the cookie so that the user does not get mixed-up limits on other queries.

It worked fine until I had to change servers, from one hosting provider to another, and now its not working and I have already build upon this mechanism.

I'm guessing maybe the PHP version matters or some php.ini config is different that makes the original server work, but not the second one. Sadly, I cant just finish the project on the first one. Any clues or suggestions?

Here's the code:

// GET LIMIT, PLACE IT ON A COOKIE
if (isset($_GET['l'])) {
    $cookie_name = "limit";
    $cookie_value = $_GET['l'];
    setcookie($cookie_name, $cookie_value, time() + (86400 * 1), "/bk"); // 86400 = 1 day
    header('Location:admin.php');
    die;
}

Then, in "admin.php":

// GET COOKIE VALUE, DELETE COOKIE
if(isset($_COOKIE['limit'])) {
    $limit = $_COOKIE['limit'];
    setcookie("limit", '', time() -3600, "/bk"); 
} else {
    $limit = 0;
}

Then I do my query.

In the last server, for example, if the limit was 10 - by the end of the second block of code, $limit would equal 10 - but in this new server, $limit is 0. I did some testing, and second If is working, it does get triggered but by the end of the if, $limit equals 0.

Help?

584

Answer

Solution:

have you tried changing thesetcookie(..) line ?

like setting the cookie without specifying a domain or setting a global one

also I think somevar_dump($_COOKIE) at the beginning of "admin.php" can help.

805

Answer

Solution:

Maybe someone can explain why...

I changed the limit variable on my URL "l" for "lim" and that made the difference.

The cookie is getting written and read without a problem. I ignore why, somehow when the URL read page.php?l=10 the $_GET order was getting skipped.

I changed it to page.php?lim=10 and now it works as it did on the first server.

Any comments as to why would be appreciated.

People are also looking for solutions to the problem: javascript - Checking the input fields in html table is empty or not for adding dynamic rows

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.