Archive for the ‘PHP’ Category.

Get current URL in PHP

Here is a function for getting the URL of the page being processed in PHP, the function accepts one optional parameter $ignore_port_80 (boolean) to tell it whether it should ignore the port part of the url if the port used is 80:

# Prototype
# string get_current_url([boolean $ignore_port_80])

# Function
function get_current_url($ignore_port_80 = true) {
$is_https = $_SERVER['HTTPS'] [...]

How to modify PHP include_path… Different approaches

What is PHP include_path?
include_path is a PHP core directive, it stores a directory path or a directories paths  list separated by colons “:” on Unix and MAC platforms or by semicolons “;” on Windows platforms.

When you use include(), include_once(), require(), require_once(), fopen(), file(), readfile() or file_get_contents() to load a file (let’s say my_file.inc.php), PHP will [...]

Install Symfony Without PEAR

A lot of web hosts have limited or nonexistent support for PEAR, the pear command will throw errors on most commands or not work at all, in order to install Symfony on such hosts you will need to download the Symfony package, copy Symfony files to your home directory and make them easily accessible from [...]

How to check a file type efficiently in PHP?

My brother asked:
Please, do you know a method in PHP to check the type of an uploaded file
without relying on extension or Mime-type because these two can be faked?
thanks
Relying on the Mime-type of a file submitted by a user is a bad practice, using the extension is even worse!
Some people use functions of the php [...]

Print HTML code from JavaScript variable

This also was about to drive me crazy so I will not let it go without writing about it..

Let’s say you have some long HTML code contained on a PHP variable, something like this

<?php

# define the $html variable using the
# Nowdoc syntax to avoid parsing

$html = <<<’HTML’
<div id=”box”>
<h1>Title</h1>
<p>Article contents + comments</p>
</div>
HTML;

# [...]

Get file extension on PHP

There are many ways to retrieve files extension with PHP, but we will have to choose one that feets our needs and gives more reliable results.
When dealing with file extension you should know that a file extension is not always 3 characters long (ex:  foto.jpeg, index.html) and that it doesn’t start after the last dot [...]

Install PHP 5.2.6 on CentOs 5.x

Oddly enough, PHP 5.2.6 is still not available on default CentOS repositories ([base], [updates], [addons], and [extras]), latest PHP available in those repos is PHP 5.1.6!

In order to install PHP 5.2.6 you can add the CentOS testing repository and enable it then install or update PHP.

To add the CentOS testing directory, create a file /etc/yum.repos.d/centos-test.repo [...]

Smarty error: Warning: preg_match() [function.preg-match]: Compilation failed: repeated subpattern is too long at offset 18454 in …

Warning: preg_match() [function.preg-match]: Compilation failed: repeated subpattern is too long at offset 18454 in /var/www/html/[...]/Smarty/Smarty_Compiler.class.php on line 454

It looks that you are running an old version of Smarty, latest version as I write is 2.6.26, you can download it from here.

How to check if a number is odd or even with PHP?

In mathematics, one of the definitions of odd and even number is that they can be expressed like this (where n is the number and k is an integer):
Odd numbers: n = 2k +1
Even numbers: n = 2k
In PHP, we can check a number’s parity by checking the remainider of the division of this number [...]