Short Tag – Web Development Blog

Entries for the ‘PHP’ Category

Post to twitter with PHP, twitting with PHP

I wrote a post about using file_get_contents() in PHP with context stream a while ago. I thought I’d post a usage example. Below is a function that posts your message into your twitter profile directly from PHP.

function myTweet($username, $password, $msg)
$context = stream_context_create(array(
‘http’ => array(
‘method’  => ‘POST’,
‘header’  => sprintf("Authorization: Basic %s\r\n", base64_encode($username.’:’.$password)).
"Content-type: [...]

Get month, day, year from timestamp functions [PHP]

Today I thought I can share you a few of my functions. Today’s article is an easy one, but I use the functions in almost all my websites, so I thought they can be useful to you.
So here are a few functions that get the year,month,day,hour,minute,second from a time stamp, similar to the MYSQL functions. [...]

PHP constants

The constants in PHP (and in any other programming language) are used to store data.
So, what’s the difference between variables and constants?
They both store data, but a variable can be modified anywhere in the code, and a constant cannot be modified. You can try to modify it, PHP won’t return any error, but it won’t [...]

CGI script and PHP

CGI it’s not a programming language. CGI(Common Gateway Interface) it’s just an interface between the http server and the software residing on the host machine.
You can write CGI script using a variety of programming languages: PHP, PERL, C, C++, etc.
Why use CGI scripts?
CGI scripts are very useful when you need to combine PHP with other [...]

Force browser to download file (not open): jpg, pdf, mp3, etc.

So, you know that modern browsers open media files (and others). For example if you have installed Quicktime then most of the browsers will open mp3 files instead of downloading them, every browser will open image files(jpg, gif, png) instead of downloading them. If you want to force the browser to download the specified files [...]

[PHP] Get file and directory list from a folder

If you need to get the list of files and directories from a folder you should use this piece of code:

$mydir = opendir("directory");

while($entryName = readdir($mydir)) {
$dirArray[] = $entryName;


Now $dirArray contains a list of all files and folders in the given directory. This array contains even the two pointers each directory has: ‘.’ (current directory) and [...]

PHP redirects, 301 redirect with PHP

You might know that there are 2 kinds of redirects. A temporary redirect and a permanent one. The user or programming experience does not change if you are using one or the other but choosing the right redirect type has a great importance for SEO.
301 – permanent redirect
302 – temporary redirect
The idea is that permanent [...]

file_get_contents() timeout in PHP

Though file_get_contents is not so advanced as CURL we can use it in lots of situations. In case you use file_get_contents and you need to set for how long to try to make connection you can use stream context.
First we create the stream context:
$sc = stream_context_create(array( ‘http’ => array( [...]

Override functions in PHP

IF you have APD (Advanced PHP Debugger) extension enabled in PHP you can override a function (or more).
The function that helps you achieve this:
bool override_function ( string $function_name , string $function_args , string $function_code )
override_function(‘myfunction’, ’$first,$last’, ’echo ”Result: “; return $first * $last;’);
This can be very handy when you have an entire script that [...]

Datediff function in PHP (similar to VB)

A function that calculates the difference between 2 dates.  It is very similar to the datediff function available in Visual Basic.

function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {

if (!$using_timestamps) {

$datefrom = strtotime($datefrom, 0);

$dateto = strtotime($dateto, 0);


$difference = $dateto – $datefrom; // Difference in seconds

switch($interval) {

case ‘yyyy’: // Number of full years

$years_difference = floor($difference / 31536000);

if [...]