1

I am try to use php to check if a cookie is set. If it is not I want some script to be output. If it is set I don't want it to do anything.

I am not a programmer and have no clue where to start with this. I have done lots of searches on google to no avail.

Cookie name = subscribed

The javascript source required for output is mysite.com/js/subscribe.js

Thanks

1

1 Answer 1

2
if(isset($_COOKIE['subscribed']))
{
    // if cookie is there
    $subscribe = false;
}
else
{
    // if cookie is not there
    $subscribe = true;
}

You could use a basic isset check first, somewhere in the top of your file before output. Then in your head, in html do something like this:

<head>
 <!-- standard head meta here -->
<?php if($subscribe): ?>
<script src="http://mysite.com/js/subscribe.js"></script>
<?php endif; ?>
</head>

You could also just run this inline, or use javascript to check cookies. This is just one example and it is handled like this in case anything else needed to be processed, unset, or anything else that would make sense of having cookies checked before output is sent.

shorter version you could use in place of the longer conditional above:

$subscribe = isset($_COOKIE['subscribe']) ? false : true;
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately this does not work. The script is still output whether the cookie is set or not. As I said, I am not a programmer so I am not sure why it prints the script anyway.

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.