Reputation: 11
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
Upvotes: 1
Views: 88
Reputation: 18843
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;
Upvotes: 2