How to check if like button has been clicked - php

I have a like button on my site, can I check if the user clicked it in the past? I dont want the user will login to facebook through my website, I want to check it without asking for permissions from the user. Is that possible? and how can I do it?

I think this can be done by saving a cookie when your user clicks like button on your page. Facebook has some event callbacks of which you can read more here
https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/
The code looks something like this:
FB.Event.subscribe('edge.create', function(response) {
if(response) {
var name = 'UserLikesMyPage'
var date = new Date();
date.setTime(date.getTime() + (14 * 24 * 60 * 60 * 1000));
var expires = '; expires=' + date.toGMTString();
document.cookie = name + '=true' + expires + '; path=/';
}
}
Than you can check if this cookie is already set.
if (document.cookie.indexOf('UserLikesMyPage') >= 0) {
alert('I already like your page')
};
And don't forget to remove the cookie if user unlikes your page. This can be found out by the edge.remove event

Related

redirect main domain to subdomain manually select by visitor

I have two wordpress website. One installed in main domain and another one installed in sub-domain under main domain. I'm doing this to manage 2 different language. I know there are so many plugins to manage multi language in site. But I have to do this manually.
But now i need to done a small job. I want to show a notification when any visitor will visit my main site for the 1st time. There I'll give 2 Language option to choose.
Option-1 Language English for main domain. (example.com)
Option-2 Language Spanish for Sub domain. (sp.example.com)
When visitor will select Option-1 he will stay in main site(domain). If he select Option-2 then he will be redirected to the another site(sub-domain) and after this it'll happen automatically every time he visits
main site(domain)
. But visitor can manually come back to main domain from sub-domain.
Any solution will be highly appreciated. Thanks
Assuming you already have a pop up asking which version they want to see, and two buttons, or links, to take them to the appropriate site... you can use jQuery or plain JavaScript:
jQuery( document ).ready( function() {
var $days = 30;
var $date = new Date();
$date.setTime($date.getTime() + ($days * 24 * 60 * 60 * 1000));
var $expires = "; expires=" + $date.toGMTString() + '; path=/';
jQuery( '#id_of_english_button' )
.click( function( event ) {
document.cookie = 'language=english' + $expires;
});
jQuery( '#id_of_spanish_button' )
.click( function( event ) {
document.cookie = 'language=spanish' + $expires;
});
});
Then you'll have a PHP cookie variable you can work with: $_COOKIE['language']
Use what DarkBee had said in his comment, and do a header('location : sub.domain.com'); to send people to the sub domain if their cookie equals 'spanish'.
We can do this job by Cookie. I am assuming that your default landing domain is English one.
In functions.php of English domain write following code
add_action('init','is_new_visitor');
$is_new_visitor = false;
function is_new_visitor(){
check_new_visitor();
set_new_visitor();
}
function check_new_visitor(){
global $is_new_visitor;
//check if cookie set
if(isset($_COOKIE['new_visitor_lang']) &&
in_array($_COOKIE['new_visitor_lang'],array('en','sp'))){
$lang = $_COOKIE['new_visitor_lang']; // get cookie value
//if language is spanish then redirect user to spanish site
if($lang=="sp"){
wp_redirect('spanish lang domain');
exit;
}
}else{
//set global variable to true if it is new visitor
$is_new_visitor = true;
}
}
function set_new_visitor(){
// check the get param and redirect user to selected domain
if(isset($_GET['lang']) && in_array($_GET['lang'],array('en','sp'))){
setcookie( "new_visitor_lang", $_GET['lang'], time() + (365 * 24 * 60 * 60) );
if($lang=="sp"){
wp_redirect('spanish lang domain');
exit;
}
}
}
Now where you have to show the links, add following code (Some where in header.php)
global $is_new_visitor;
if($is_new_visitor){
echo 'English';
echo 'Spanish';
}
/*this code write on your main site and the user will
automatically redirect to the subdomain */
if(!isset($_SESSION['lang'])
{
$_SESSION['lang']='visitor selected language';
}
if($_SESSION['lang']=="Option-2")
{
header("Location:url");
}

How to get a cookie value for input field?

I have a form and I have set cookies to the input fields. I can find the cookies in resources when I go to inspect element. The code is confidential so I can't show how I set my cookies. But I am sure that I can find the selected input fields in resources->cookie.
The same form appears in all the pages. When I redirect from one page to other page the form fields which I selected must appear in all the pages.
I used the below code for getting the cookie value
<script type="text/javascript">
$(document).ready(function() {
if(input1 = getCookie("input1 "))
document.myform.input1.value = input1 ;
});
</script>
but I am getting error as Uncaught ReferenceError: getCookie is not defined
Can anyone suggest what would be the reason for this error? and how do I get the get cookie value to the input field?
Usually you should google it how to set cookie, not sure if you declare the function as something like getCookie?
<script type="text/javascript">
function setCookie(key, value) {
var expires = new Date();
expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}
$(document).ready(function() {
setCookie("input1",'1');
alert(getCookie("input1"));
document.myform.input1.value = getCookie("input1");
});
</script>
And Here is the Fiddle http://jsfiddle.net/hr4mubsw/5/
For more information check this one How do I set/unset cookie with jQuery?
Hope it may help :)

Proper Date Formatting for Cookies

So I am trying to do what I think is the impossible. I want to destroy all session cookies on browser or tab close, so I came up with a solution, I am just having small issues with JavaScript formatting. This does have to be time based, as when the next page reloads, it will put the timer for expiration for a year, so that other pages on my site don't delete the cookies as well. So until they navigate away from the site or close the tab, the cookie wont expire.
So The JavaScript starts like this:
var today = new Date();
today.setSeconds(today.getSeconds() + 5);
alert(today);
window.onunload = function(){
document.cookie = 'PHPSESSID=; expires=' + today;
};
So When I run this, it does not recognize the today variable, or I am not formatting today correctly.
All help is appreciated!
The today variable is out of scope. You need to create it in the unload, or pass it in the function().
window.onunload = function(today){
document.cookie = 'PHPSESSID=; expires=' + today.getTime();
};
OR
window.onunload = function(){
var today = new Date();
today.setSeconds(today.getSeconds() + 5);
document.cookie = 'PHPSESSID=; expires=' + today.getTime();
};
So I decided to destroy the session cookies with a small script.
This is what I came up with.
window.onunload = function(){
var today = new Date();
today.setSeconds(today.getSeconds() + 2);
var today1= today.toUTCString()
document.cookie =
'PHPSESSID=<?php echo $sesid; ?>; expires='+ today1 +'; path=/'
alert(document.cookie);
};
So This will set the destroy to 2 seconds, but if you navigate to any other page on my site, it will run this first.
var today2 = new Date();
today2.setSeconds(today2.getSeconds() + 10000);
var today3= today2.toUTCString()
document.cookie =
'PHPSESSID=<?php echo $sesid; ?>; expires='+ today3 +'; path=/';
This allows the session to be destroyed anytime you navigate away from the page.

Log out automatically when there is no use of Keyboard or Mouse

When i do not use keyboard and mouse for a particular time limit (Like 10 min or 20 min) at that time it should log out User automatically from the current session. Please give me any suggestion or code in PHP.
You need javascript to detect browser events.
With jQuery, something like (untested)
var timeSinceLastMove = 0;
$(document).mousemove(function() {
timeSinceLastMove = 0;
});
$(document).keyup(function() {
timeSinceLastMove = 0;
});
checkTime();
function checkTime() {
timeSinceLastMove++;
if (timeSinceLastMove > 10 * 60) {
window.location = "path/to/logout.php";
}
setTimeout(checkTime, 1000);
}
you must set the session timeout in your code
session_set_cookie_params(3600); // sessions last 1 hour
session_start(); // do this after setting the params

jQuery - saving/using cookies

I am currently developing a shopping cart solution and I have a field, which is used for tax.
What I'm looking to do, is when the user selects the checkbox field for tax, then this is stored in the cookie, so when they go to other pages and then return to the cart, then these fields are checked.
I am using http://plugins.jquery.com/project/Cookie to achieve this.
I can create the cookie for the checkboxes, but I am struggling to then check the cookie and if it was for a checkbox, then set the checkbox as 'checked'.
I also need, the deletion of cookies. When the user unchecks the box, to the call something like $.cookie("vat_1", null)
I have the following code so far:
$(document).ready( function() {
$('input:checkbox[name="<?php echo $prod; ?>"]').click(function() {
var name = $(this).attr("name");
var value = $(this).val();
var date = new Date();
date.setTime(date.getTime() + (30 * 60 * 1000));
$.cookie(name, value, { expires: date });//Set the expires time as per your requirement.
cookie = $.cookie(name);
alert(cookie);
})
});
All help is muchly appreciated.
Thanks
To support loading the previously checked boxes, at the top of the document.ready() function, insert the following code:
$('input:checkbox').each(function(index) {
cookie = $.cookie($(this).attr("name"));
if (cookie == 1) {
$(this).prop('checked', true);
});
This should loop through each checkbox on the page, lookup the relevant cookie, and alter the checkbox accordingly.
In terms of deleting the cookies and setting them correctly in the first place, you should not be using the val() of the checkbox. Instead you should be looking up its checked property:
$('input:checkbox[name="<?php echo $prod; ?>"]').click(function() {
var name = $(this).attr("name");
var value = $(this).prop('checked');
var date = new Date();
date.setTime(date.getTime() + (30 * 60 * 1000));
if (value) {
$.cookie(name, 1, { expires: date });
} else {
$.cookie(name, null);
}
cookie = $.cookie(name);
alert(cookie);
})
These two pieces of code should be combined as such
$(document).ready( function() {
//first code above
//second code above
});

Categories