delete or clear cookie using javascript or php [duplicate] - php

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);

Related

How I can use the Javascript variable in PHP? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have that code:
function saveData(position, map) {
var latlng = marker.getPosition();
var late = latlng.lat();
var longe = latlng.lng();
<?php
require("sql.php");
session_start();
$name = 'prova';
$address = 'temare';
$type = 'bar';
$late = "<script>document.getElementByID('late').value</script>";
$longe ="<script>document.getElementByID('longe').value</script>";
I need to get late and longe variables in PHP but this doesn't work, anyone knows how I can do it?
You can't access javascript variables using PHP as JavaScript is executed on client side ( browser ) and PHP on server side.
The best what you can do is send data from those variables as request to server and read them, for example using jQuery ajax request:
Good example you can find here:
jQuery Ajax POST example with PHP
Or you can use this:
javascript
function sendData(variable1, variable2) {
$.ajax({
type: 'post',
url: 'myphpscript.php'
data: 'var1='+variable1+'&var2='+variable2
});
myphpscript.php
<?php
$variable1 = $_POST['var1'];
$variable2 = $_POST['var2'];
<script type="text/javascript">
<?php $abc = "<script>document.write(late)</script>"?>
<?php $def = "<script>document.write(longe)</script>"?>
</script>

Set Session inside the javascript [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I want to set session inside the javascript. But all the time this function return false value. Please tell me the error of this code.
<script>
function myFunction(){
var r=confirm("Would you like to add another item ?");
if (r==true) {
alert('ok');
var varname = '<?php echo $_SESSION["redirect"]=1; ?>';
}
if(r==false) {
alert('bad');
var varname = '<?php echo $_SESSION["redirect"]=2; ?>';
}
}
</script>
You can't evaluate PHP on the client. Do an AJAX request to the server setting the session variable instead.
Javascript runs on the client side, so you'll need to perform an AJAX call to a server side script that will change the sessions value.
Something in the lines of:
function changeSession()
{
$.ajax({
url: /change_session.php?value=1&key=redirect,
dataType:"html"
});
}
And in change_session.php:
<?php $_SESSION[$_REQUEST['key']]=$_REQUEST['value'] ?>

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.

Javascript php variable not passing with http_build_query [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
Why does this not work?
var data1 = "<? http_build_query($_GET); ?>";
var data2 = "buy.php?";
var url = data2+data1
document.getElementById('framebox').src = url;
Thanks.
Because data1 is empty (PHP is not outputing anything), try:
var data1 = "<?= http_build_query($_GET); ?>"; // or
var data1 = "<?php echo http_build_query($_GET); ?>";
Any reason you're using PHP to build the query string instead of doing it directly in Javascript?
You can also archive it the plain old pure javascript way:
var data1 = location.href.split('?').pop();
var data2 = "buy.php?";
var url = data2+data1
document.getElementById('framebox').src = url;
But it's much more fun to mix stuff.....

Store Form Values In Cookie

I've been trying to teach myself about cookies for the last couple of hours and how I would go about storing values from a few form fields into a cookie.
Im not having much luck, all the examples I've found havent been very helpful.
Should I generate them using PHP or JS?
Any feedback or a kick in the right direction would be highly appreciated!
http://jsfiddle.net/yucM7/
Thanks in advance!
Here's an example.
All you need is jQuery and cookie plugin
Keep in mind, there're couple of changes in html code.
$(document).on('submit', '#myForm', function() {
// serialize our form (get string containing field names and values)
dataString = $(this).serialize();
// set new cookie
$.cookie('formCookie', dataString);
return false;
});
$(document).on('click', '#getCookie', function() {
// get serialized string from cookie
cookieData = $.cookie('formCookie');
// if cookie exists continue
if (cookieData != null) {
// split cookieData string into an array of fields and their values
cookieArray = cookieData.split('&');
// go through each field and split it too to get field name and it's value
$.each(cookieArray, function(k, v) {
field = v.split('=');
// populate field with data
$('#myForm [name="'+field[0]+'"]').val(field[1]);
});
}
return false;
});
You can set cookie using Javascript by following (Refer http://www.w3schools.com/js/js_cookies.asp):
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
Setting cookie using Jquery: $.cookie("example", "foo");
Alternatively you can set your cookie from server by :
<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);

Categories