PHP: Print the current year for copyright notices June 20th, 2009
Hardcoding the year in a copyright notice is an easy way to get a blog or other website started, just placing the starting and ending years next to the copyright symbol. We have the very best of intentions to change this number right after the big ball drops in Times Square each year. We’re also human and, all too often, I see websites with copyright notices that are years behind. If your website is PHP-powered, this quick bit of code will put the current year in your copyright notice and off of your mind.
To change your copyright notice, or anything else you’d like that should display the current year, you must first find the script in which the copyright display is generated. If you are using a WordPress blog, for example, this is likely in your footer.php file in the directory of the theme you are using. If you aren’t sure which file holds this, you could try searching for the text © in all of the scripts that make up your website. This is the code that generates the copyright logo and should lead you directly to the file(s) involved.
Once you’ve found this, there are two possibilities for including the year information depending on how the script is setup. It is possible that the script is sending the line including the copyright information from an ‘echo’ command in PHP, something like:
echo ‘© 1812–2006 Olde Time Blogs’;
In this case you need to modify this PHP echo command so that it calls the PHP date command. Note that the command could also work with double quotes instead of single quotes, just be sure to keep using the same kind.
echo ‘© 1812–’.echo date(‘Y’).’ Olde Time Blogs’;
In PHP, the period symbol is used to stitch together strings, so the command above makes one string out of three.
The other possibility is that the PHP script is formatted as HTML content with separate PHP instructions enclosed by opening/closing PHP tags. This is more proper and common in applications like WordPress. In this case, inserting a dynamic, ever-correct year is a matter of changing:
© 1812–2006 Olde Time Blogs
to
© 1812– Olde Time Blogs
courtesy http://www.tech-recipes.com/rx/2815/php_print_the_current_year_for_copyright_notices/
PHP: Delete or expire a browser cookie June 20th, 2009
Cookies may be created with an expiration time. It may become necessary to expire a cookie before the expiration time such as when a user logs out of a cookie-based authentication web application. This recipe describes expiring a cookie.
Expiring a cookie uses the same command as creating a cookie. The cookie value is left blank and the expiration time needs to be in the past. To expire the cookie ‘user’ use:
setcookie(‘user’,”,1);
The second parameter is the cookie value and is set to blank with the double quotes. The third parameter is the expiration time. A value of zero here indicates that the cookie is to remain valid until the browser is closed. A positive value indicates the time at which the cookie is to expire. Many sources indicate using something like
setcookie(‘user’,”,time()-3600)
which sets the expiration time 3600 seconds (1 hour) in the past. The trick is that this references the time on the server while the cookie expiration is dependent on the time of the host running the browser. If there is a mismatch of time between the two hosts, it is possible that it a cookie may not expire. However, using a time of ‘1′ indicates an expiration time of 1 second after midnight, January 1st, 1970 which is the earliest possible expiration time.
courtesy http://www.tech-recipes.com/rx/1501/php_delete_or_expire_a_browser_cookie/
PHP: Read a browser cookie value June 20th, 2009
Once your web application has set a cookie in a user’s browser, it is a simple matter to retrieve that value in subsequent page requests. This recipe describes reading a value from a cookie in PHP.
After a cookie with a name of ‘user’ has been set in a previous page, the value can be accessed with the global array $_COOKIE like this:
$user = $_COOKIE['user'];
Since this is a normal array, anything you can do to an array like iterate through it one value at a time. For debugging purposes, it is often useful to use this quick trick to see the whole contents of an array:
print_r($_COOKIE);
courtesy http://www.tech-recipes.com/rx/1500/php-read-a-browser-cookie-value/
PHP: Set or create a simple browser cookie with setcookie June 20th, 2009
Web applications overcome the lack of session support in the HTTP protocol primarily through the use of cookies. Transparent to the user and simple for the web developer, cookies provide the foundation of keeping up with who a web user is, where they’ve been, and what they had for lunch. Well, who and where, anyway.
The built-in PHP function setcookie() creates or updates a cookie name/value pair in the user’s browser. A cookie contains information sent by a web server to a user’s browser. Whenever a web page is accessed at that server that meets the requirements, cookies associated with that server (that haven’t expired) are sent back without modification to the server.
The basic cookie consists of a name and a value. For example, a cookie with name ‘user’ can be set to the user’s username, possibly encoded for security. To set the cookie user with the value ‘qmchenry’ use this command:
setcookie(‘user’,'qmchenry’);
This cookie has a default expiration time of 0 which means it will persist until the browser is closed.
It is important to note that cookie information is sent in the web page response headers and, like other times PHP needs to change header values, the setcookie function must be called before any output is sent to the user.
courtesy http://www.tech-recipes.com/rx/1499/php-set-or-create-a-simple-browser-cookie-with-setcookie/
PHP: Rename or move a file on the server June 20th, 2009
Working with server-side files with PHP adds a great deal of flexibility to some applications. A common scenario when working with files involves creating a temporary file through uploading or other means and then renaming this file to a permanent location. This recipe describes changing the name of an existing file using the PHP function rename().
The syntax for the PHP rename function is consistent with that of the UNIX mv function, which always makes things more comfortable. The general syntax is:
rename(‘/path1/old_filename’, ‘/path2/new_filename’);
The rename function returns true if the rename was successful, and false otherwise.
Just like with the unlink function which deletes files, the rename function most likely fails because of file permissions. To rename a file, the directory containing it must be writable by the user trying to rename it. The file’s permissions (and ownership) are irrelevant.
courtesy http://www.tech-recipes.com/rx/1492/php-rename-or-move-a-file-on-the-server/
