How to Unset/Remove a Cookie Immediately with PHP (Even for the Current Request, Without Having to Refresh the Page)

php unset cookie

Setting cookies is generally done using setcookie(), that’s a known fact. However, unsetting them generally is a bit trickier, since the cookie remains available throughout the current request, if requested via the global variable:

$_COOKIE['the_cookie_name']

The cookie is fully removed/expired on the next page refresh.

The function below will avoid this behavior. It will still remove the cookie on the next page request, but it will also make it unavailable during the current request too, essentially “expiring”/”removing” it immediately.

function removeCookie($cookieName){
    unset($_COOKIE[$cookieName]);
    setcookie($cookieName, '', time() - 3600, '/');
}

To remove e.g. a cookie named “last_username”, you would call:

removeCookie("last_username");

Image credit to @Ginny


How to Insert a MD5 Password (or the Result of Any Other MySQL Function) Into a Field with “Navicat for MySQL”

MySQL supports MD5() and some other cool functions that do the math for you and save the result in the table field, but what happens when you already have a database and the table in place and you are using “Navicat”, plus you don’t want to fiddle with raw SQL?

Luckily, Navicat has the so called “Raw Mode” when viewing a table’s rows which allows us to insert data using MySQL expressions/functions.

Here are the steps on how to insert a MD5()’ed password in a “users” table using Navicat:
1. Open your desired table with Navicat:

navicat raw 1

2. Choose “View->Raw Mode” from the top menu:

navicat raw 2

3. In your password field, type the expression, whose result you want inserted in that field, e.g.

MD5('newpassword')

navicat raw 3

4. Hit “Enter” and “Ctrl+S” to apply the changes. Optionally disable “Raw mode” from the menu if you need to go back to the regular mode.

navicat raw 4

Enjoy the result of the MD5, without using complex raw SQL queries that are prone to typos.


How to get a link to the still image of a YouTube video?

YouTube has multiple servers for serving images of the videos, hosted on the site. They are mainly used for load ballancing (so you get the image that is hosted on a server, that is physically closest to you so it loads faster).

However, as a developer or a website owner you may want to programmatically build a link to the thumbnail or still image that YouTube uses for a given video.

What you need is – the ID of the video (e.g. if the video in question is http://www.youtube.com/watch?v=m3JAtvsqfF8, the ID would be m3JAtvsqfF8). When you have the ID of the video, just replace it in the below URL and open it in your browser.

http://img.youtube.com/vi/video_id_here/0.jpg

You should immediately see the full-size thumbnail that YouTube uses across its website (note that scaled down versions are also available; just replace the last zero in the URL with “1.jpg”, “2.jpg” or “hqdefault.jpg”).


Counting the Number of Options/Elements in a <SELECT> Dropdown with jQuery

Let’s say you have this (very) simple <select> dropdown:


And one day you decide to figure out how much elements or <option> tags the given <select> has.

It’s easy:

var items = $('#my_dropdown option').length;
alert(items);

The above should immediately display “2” since we have 2 countries in the select dropdown.