PHP Logo

Getting The Current URI In PHP

15th April 2008 - 2 minutes read time

The $_SERVER superglobal array contains lots of information about the current page location. You can print this off in full using the following line of code.


echo '<pre>'.print_r($_SERVER, true).'</pre>';

Although this array doesn't have the full URI we can piece together the current URI using bits of the $_SERVER array. The following function does this and returns a full URI.


function currentUri(){
 $uri = 'http';
 if(isset($_SERVER['HTTPS'])){
  if($_SERVER['HTTPS'] == 'on'){
   $uri .= 's';
  };
 };
 $uri .= '://';
 if($_SERVER['SERVER_PORT'] != '80'){
  $uri .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
 }else{
  $uri .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
 };
 return $uri;
}

You can use this function like this:

PHP Logo

The Final Keyword In PHP5

14th April 2008 - 3 minutes read time

PHP5 allows you to stop classes being extended or to stop child classes overwriting functions.

The first way to use the final keyword is to stop child classes from overwriting functions when they are created. This can be used to stop an important function from being overwritten. To use the final keyword here just add it to the start of function name.

class ParentClass{
 final public function importantFunction() {
  echo 'ParentClass::importantFunction()';
 }
}
 
class ChildClass extends ParentClass{
 public function importantFunction() {
  echo 'ChildClass::importantFunction()';
 }
}
 
$child = new ChildClass();
$child->printString();

Attempting to override this function will produce the following error.

MySQL Logo

Get MySQL Version Information Through PHP

13th April 2008 - 2 minutes read time

There is little syntactical difference between MySQL 4 and MySQL 5, but sometimes finding that difference can pinpoint a bug. The mysql_get_server_info() function will tell you what version of MySQL you are using. You can call it with no parameters, in which case it picks the most recently created MySQL resource, or with the resource handle created with mysql_connect().

Here is an example of how to use it.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
echo mysql_get_server_info();

You can achieve the same effect with a simple MySQL query.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$query = mysql_query('SELECT VERSION() as mysql_version');
PHP Logo

Using Multiple Arguments To A Function With parse_str() In PHP

12th April 2008 - 3 minutes read time

Sending multiple arguments to a function can be done using a parameter string. This is just like a URL that has data encoded into it. For example, if you wanted to send two parameters (called parameter1 and parameter2) to a function then you would use the following string.


parameter1=value1&parameter2=value2

To use this in the function you create the function as normal with a single parameter. This single parameter is the string that will contain all of your arguments.


function test($arguments)
{
}

You must run the parse_str() function on the arguments parameter to extract the data you need. You can then call the parameters by their names as variables.

Linux Logo

View Directory List After Entering

11th April 2008 - 2 minutes read time

When navigating the file structure in Unix/Linux environments you will often find yourself typing cd to change the directory and then immediately typing ls to see the contents of the directory.

It is possible to run ls automatically every time you run cd by adding the following commands to your .bashrc file.

cd() {
 if [ -n "$1" ]; then
  builtin cd "[email protected]" && ls
 else
  builtin cd ~ && ls
 fi
}}

 

Linux Logo

Simple Trick To Run Last Command As Sudo

10th April 2008 - 2 minutes read time

You can often forget what you are not running as a super user, so if you type in a command that you can't run with your current set of privileges it will tell give you a permission denied response.

An alternative is to use the !! command to run the last command in the .bash_history. Use this with the sudo command to run the last command as a super user.

> command
Permission denied
 
> sudo !!
PHP Logo

Faster Way Of Checking String Length With PHP

9th April 2008 - 2 minutes read time

If you want to know the length of a string in PHP you would normally turn to the strlen() function which simply tells you the length of the string.

$string = 'this is a string';
$strLength = strlen($string);

To use this to check the length of the string use the following example.

$string = 'this is a string';
if (strlen($string) < 20) {
 // code here
}

A quicker way of looking at the length of a string would be to use the isset() function in conjunction with the curly braces {} used for locating character from a string.

$string = 'this is a string';
if (!isset($string{20})) {
 // string too short
}

Because isset() is a language construct it works quicker than strlen() so this comparison has almost no overhead at all.

PHP Logo

PHP Filter FILTER_VALIDATE_URL Limitations

8th April 2008 - 3 minutes read time

I have previously talked about the filter functions available in PHP5, but failed to spot this limitation when I was doing the research for those articles. It appears that the filter to validate URL string, namely FILTER_VALIDATE_URL, is not really adequate to the task.

Take the following examples of the filter in an if statement.

if ( filter_var($url,FILTER_VALIDATE_URL) ) {
 return true;
}else{
 return false;
};

This will return true if the URL is valid and false if the URL is invalid. To test this I plugged the following URL strings into the function and recorded each of the outcomes.

PHP Logo

Capture A Website As Image With PHP

7th April 2008 - 5 minutes read time

One thing that comes in useful when linking to other websites is to use a little thumbnail of the site as part of the link. Although it is possible to do this it usually involves having command line access to the server so that you can install various different programs and extensions.

The simplest mechanism accomplish this is to use third party sites to create the thumbnail for you. Here are a few examples of free thumbnail services.

www.thumbshots.org

Thumbshots provides access to small images (thumbnail size) of many different websites. However, they are very small, with no way to change this, and can often be weeks out of date if the site has had a redesign. You can access Thumbshots from any webpage by using an image with a source that reads from the Thumbshots website.

PHP Logo

Update Or Insert A Row With MySQL And PHP

6th April 2008 - 4 minutes read time

Many situations arise where you need to either insert or update some data in a table but which you will not be certain as to which function to perform. A common solution is to do a query on the table first to see if the data exists and then insert if it doesn't and update if it does. However, this creates an unnecessary overhead in that every time the code is run at least 2 queries are run.

A better way is to try to update the table and then use the mysql_info() function to detect how many rows where updated in the query and how many rows matched the parameters in the update query.

Take the following query.


UPDATE table SET value = "value" WHERE valueId = 2;

When run on a table the mysql_info() function returns the following result.