get snippets

How to set a cookie, remove a cookie and get the value of a cookie in PHP

Tagged php, cookie, management, set, remove, get  Languages php

PHP cookie management is a good example of how incredibly badly designed PHP is:

Set a cookie that expires when browser is closed

Note that you most probably want to set the path, which we do here:

$value = 0;
$expires = 0;
setcookie('cookie_name', $value, $expires, '/');

Remove cookie

setcookie ('cookie_name', FALSE, time()-10000);

Get cookie value

$_COOKIE['cookie_name']

DELETE, PUT, GET and POST with jQuery

Tagged delete, put, get, post, jquery  Languages javascript

Example of how to do DELETE, PUT, GET and POST with jQuery and Rails REST type routing:

$.ajax({ 
    type: 'get', 
    url: '/subscriptions/' + id,
    data: "subscription[title]=" + encodeURI(value),
    dataType: 'script' 
}); 

$.ajax({ 
    type: 'POST', 
    url: '/subscriptions/' + id,
    data: "subscription[title]=" + encodeURI(value),
    dataType: 'script' 
}); 

$.ajax({ 
    type: 'PUT', 
    url: '/subscriptions/' + id,
    data: "subscription[title]=" + encodeURI(value),
    dataType: 'script' 
}); 

$.ajax({
    type: 'DELETE',
    url: url,
    data: 'subscription[id]=' + id,
    dataType: 'script'
});

The gotcha with DELETE is to specify the ID using the data parameter, otherwise you might get this error; at least with Rails 2.2.2, jQuery 1.3.1, FF 3:

/!\ FAILSAFE /!\  Mon Feb 16 13:57:52 +0200 2009
  Status: 500 Internal Server Error
  You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.attributes
    /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb:708:in get_attributes'
    /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb:462:in collapse'