Proper Date Formatting for Cookies - php

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.

Related

Javascript, create cookies, and a function that will read, and increment the cookies

I have an HTML webpage saved on my desktop that uses iFrame.
The src for the iFrame points to a page on the internet, like this:
www.example.com/results.html?id=1&year=1900&month=1&x=1&y=2
I have a button on my webpage that changes the value of month and year when clicked.
So when month gets to 13, it changes back to 1 and sets the year ahead by 1. Else, it increments until it reaches 13.
if (month <= 12) {
month++;
}
if (month == 13) {
month = 1;
year++;
}
Great that works, but when you refresh the page the variable goes back to whatever it was set to by default so 1900 and 1.
$.cookie.set('month', month)
document.cookie = NameOfCookie + "=" + month;
I need the cookie to:
Use nextPage() function to change the iFrame's src
function nextPage() {
document.getElementById("main").src="http://www.example.com/results.html?id=1&year=" + year + "&month=" + month + "&x=1&y=2";
}
I don't care about the expiration date, the path should just be that one webpage, and the domain is actually the webpage saved on my hard drive.
Cookies in Javascript are made easier with functions design to read/write them.
Peter-Paul Koch has an example set of said functions on www.quirksmode.org.
They look like this:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Alternatively, you could deal with document.cookie yourself, or you could use the Web Storage API to store your data. (Note that Web Storage may not be supported on older browsers)
You can test if the Web Storage API is available like this:
if (window.localStorage) {
// Web Storage is available
} else {
// Web Storage is not available
}
To store something:
// The first parameter is the name, the second parameter is the value to store
localStorage.setItem("month", "3");
To read the value back:
var m = localStorage.getItem("month");
alert("month = " + m);
For more information on Web Storage, see the Mozilla Developer Network.

How to check if like button has been clicked

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

delete or clear cookie using javascript or php [duplicate]

This question already has answers here:
Clearing all cookies with JavaScript
(26 answers)
Closed 9 years ago.
I want to javascript or php script delete the cookie that i was set up with javascript now i want to delete that cookie.
here this is my javascript:
<script type="text/javascript">
var url = window.location.hash;
var result = url.split('?');
var advertise = result[1].split("&");
var id = advertise[0].split("=");
document.cookie = "update_id=" + id[1];
var name = advertise[1].split("=");
document.cookie = "update_name=" + name[1];
</script>
how to delete or clear this cookie using javascript or php code.
You can do like this in JavaScript :
Just call delete_cookie function by passing your cookie name.
function delete_cookie(cookie_name)
{
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime (cookie_date.getTime() - 1);
document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
You can do like this in PHP :
setcookie("cookie_name", "", time()-3600);

Calculate time passed variable to use in $.get()

I'm trying to update my database with some information. One of the key pieces of information is how much time has passed since the page first loaded and when the user click a button. My code looks like this:
<script>
function pauseVideo() {
$.get("video_pause.php?pause=" + timePassed + "&videoid=<?php echo $_GET['sessionid']; ?>&sessionid=<?php echo $_GET['videoid']; ?>");
}
</script>
and
<html>
<div id="pause" onclick="pauseVideo()">PAUSE</div>
</html>
My PHP is fine so ignore that. The part I'm having trouble with is the 'timePassed'. I need this to be the amount of time in seconds since the page was first loaded and the person clicks the PAUSE div.
I think I need to run a function on click to find the passed time and then use that time variable in the $.get() somehow?
When the document loads, just save the current time in a variable:
$(document).ready(function() {
var timeWhenLoaded = (new Date).getTime() / 1000;
});
Then, when the pause button is clicked, calculate the time that has passed:
function pauseVideo() {
var currTime = (new Date).getTime() / 1000;
// time in seconds
var timePassed = Math.floor(currTime - timeWhenLoaded);
$.get("video_pause.php?pause=" + timePassed + "&videoid=<?php echo $_GET['sessionid']; ?>&sessionid=<?php echo $_GET['videoid']; ?>");
}
Get rid of the onclick in your HTML, and remove your existing function, then put this in the head section of your page:
(function(){
var loadTime = (new Date).getTime(); // Page started loading
$(function(){
// DOM fully loaded, so move the assignment here if that is what
// you want to consider as the load time
$('#pause').click(function(){
$.get("video_pause.php?pause=" + Math.floor(((new Date).getTime() - loadTime)/1000) + "&videoid=<?php echo $_GET['sessionid']; ?>&sessionid=<?php echo $_GET['videoid']; ?>");
});
});
})();
Also note that you can never trust that variable on the server side. Anyone could input a negative number or even the word 'pizza' for the value if they really want to.
Something like:
var startTime = (new Date).getTime() / 1000;
function pauseVideo() {
var curTime = (new Date).getTime() / 1000;
var timePassed = Math.floor(curTime - startTime);
$.get("video_pause.php?pause=" + timePassed + "&videoid=<?php echo $_GET['sessionid']; ?>&sessionid=<?php echo $_GET['videoid']; ?>");
}
if the page with the following code is generated server-side, you can either just pass the current time to the script, as in:
<html>
<div id="pause" onclick="pauseVideo('" + curTime +"')">PAUSE</div>
</html>
(needs echo syntax)
or put it in a hidden field and pass it back to the server. (and do your calculations in php)
this way, you get the time passed since the page was requested...

Javascript counter on php page

I've got a java script counter on my php page. (I should probably add that I don't know java script). It display's the time the user is active on the page. My problem is if the user presses F5 or refreshes the page the counter starts from 0 again. How do I change this so that it remembers the time? Help will be greatly appreciated.
Javascript:
var pageVisisted = new Date();
setInterval(function() {
var timeOnSite = new Date() - pageVisisted;
var secondsTotal = timeOnSite / 1000;
var hours = Math.floor(secondsTotal / 3600);
var minutes = Math.floor(secondsTotal / 60) % 3600;
var seconds = Math.floor(secondsTotal) % 60;
document.getElementById('counter').innerHTML = hours + ":" + minutes + ":" + seconds;
}, 1000);
The php page
<head>
<?php
session_start();
?>
<script type="text/javascript" src="counter.js"></script>
</head>
<body>
<?php
echo "<span id='counter'></span>";
?>
</body>
You can use a cookie to do this. Since you say you don't know javascript, you might want to just review this page http://www.quirksmode.org/js/cookies.html where it tells you how to read and write cookies. Every time you evaluate the time on the site, just read/write to that cookie and it will still be there when the page reloads.

Categories