0

My script works in the browser only when I hit F5. The previous result seem to be cached or something.

This is how it looks like:

http://mywebsite.com/unsubscribe.php

This causes a serious problem. I have an unsubscribe link in my email I want send out to users but when I click it, nothing happens, because the click is considered the same when I hit enter in the browser. So even though I have a parameter in the link as

http://mywebsite.com/[email protected]

the value is not captured.

$email = $_GET['email'];
$newsletter = 'No';


    try {
        $stmt = $conn->prepare("UPDATE USERS SET NEWSLETTER = ? WHERE EMAIL = ?");
        $stmt->execute(array($newsletter, $email));
        $response["success"] = 1;
        } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
        $response["success"] = 0;
    }
    echo 'email: '.$email.'<br>';
    print(json_encode($response));

Result:

email: [email protected]
{"success":1}

But this is because [email protected] is cached (or something else) but the value is not updated in the database. Now when I hit F5, the value is updated in the database. What's wrong?

2
  • You never initialize $response as an array. You use GET variables without checking that they are set. And finally, printing stuff before the JSON makes it useless. Apr 26, 2015 at 10:40
  • I don't think that matters. This is a sample code and I know that I can't echo anything before printing json but I am currently testing in the browser. Even though I add a simple echo 'X' before everything, it doesn't get printed out unless I hit F5.
    – erdomester
    Apr 26, 2015 at 11:04

1 Answer 1

0

If the problem is the cache, you should add some headers to remove the cache. Then, you should send a whole valid JSON! (You added unvalid part with some text).

headers:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');//*/

Simple JSON for PHP -If you use this method, it automatically sends headers-

include('includes/json.php');

$Json = new json();

$email = $_GET['email'];
$newsletter = 'No';
try {
    $stmt = $conn->prepare("UPDATE USERS SET NEWSLETTER = ? WHERE EMAIL = ?");
    $stmt->execute(array($newsletter, $email));
    $response["success"] = 1;
    } catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    $response["success"] = 0;
}

$Json->add('status', '200');
$Json->add('message', '$success');
$Json->add('email', '$email');
$Json->add('response', '$response');

$Json->send();  
2
  • I used only the cache-control and expires headers and now it works in Firefox. I am still working on why Chrome handles this differently. Thanks
    – erdomester
    Apr 26, 2015 at 11:19
  • Maybe cleaning the cache of chrome would do the job. Maybe chrome stores "That webstite accept cache" ? I do not really know. :/ Apr 26, 2015 at 11:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.