Thursday, August 30, 2012

How to get full URL in PHP


To get full url of running script, we use the following two functions in PHP

  • $_SERVER['HTTP_HOST']
  • $_SERVER['PHP_SELF']

Examples
Consider my url string is : http://mysite.com/products/booking.php

<?php echo $_SERVER['HTTP_HOST']; ?>

Output : mysite.com

echo  $_SERVER['REQUEST_URI'];

Output : /products/booking.php

<?php echo 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);?>

Output : http://mysite.com/products

Thursday, July 19, 2012

Email validation using Regular Expression

Following code shows how to validate email in php.

<?php // chek email validation
if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$_POST['email']))
{
echo "invalid Email Address";
}
?>

Detect url contains query string ?

parse_url function is used to break the url components
This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
  • scheme - e.g. http
  • host
  • port
  • user
  • pass
  • path
  • query - after the question mark ?
  • fragment - after the hashmark #


The following code reads url query string and convert into array.

<?php
$queries= parse_url($_SERVER['REQUEST_URI'],PHP_URL_QUERY);

 // splits url query string into array
$queries_elements = split('&', $queries);
//converts to array
for($i = 0; $i != sizeof($queries_elements); $i++)
    {
    $queries = split('=', $queries_elements[$i]);
    //assigns to new array
    $params[$queries[0]]=$queries[1];
    }
var_dump($params);

?> 

More examples 
echo parse_url('http://example.com?mode=edit&id=34', PHP_URL_QUERY);
Output : 'mode=edit&id=34'
echo parse_url('http://example.com?mode=edit&id=34#user', PHP_URL_FRAGMENT)
Output : 'user'
echo parse_url('http://example.com', PHP_URL_SCHEME);
Output : 'http'
echo parse_url('http://example.com?mode=edit&id=34#user', PHP_URL_HOST);
Output : 'example.com'
echo parse_url('http://example.com:88?id=34', PHP_URL_PORT);
Output : 88

Tuesday, January 3, 2012

PHP - php.ini explained

PHP.ini is very useful and it is a configuration file that is used to customize behavior of PHP at runtime. This enables easy administration in the way you administer Apache web server using configuration files. The Settings in which  upload directory, register global variables, display errors, log errors, max uploading size setting, maximum time to execute a script and other configurations is written in this file.

When PHP Server starts up it looks for PHP.ini file first to load various values for settings. If you made changes in PHP.ini then you need to restart your server to check the changes be effected.

The major configuration parameters in php.ini as follows

OptionDescription
short_open_tag=<On|Off>The short open tag <? instead of <?php.
If you run old applications, enable this; this may cause confusion if other language processors like XML, etc, are present.
output_buffering=<Off|Integer|On>Maximum output data buffer size before sending to client. On enables infinite buffer size (dangerous!). Best value: 4096.
zlib.output_compression=<Off|On>Enables/disables gzip compression of output.
max_execution_time=<Integer>Maximum script execution time (in seconds), set to 0 for CLI. Should be set after trial and error, though 30-60 seconds should be good for standard applications. A very large value is dangerous; a script can hog resources for a long time.
max_input_time=<Integer>Maximum time a script can spend parsing request data. Default is unlimited (-1) [hard-coded for CLI].
memory_limit=<size>Maximum memory a script may consume; size defaults to bytes, but modifiers like M, G can be applied, like 128 M. Keep it so that malicious scripts don’t hog all available memory. 128-256 M is sufficient.
error_reportingSets the type of errors reported to stdout, stderr or the error log. Default value: E_ALL & ~E_NOTICE. Production value: E_ALL & ~E_DEPRECATED. Development value: E_ALL | ~E_STRICT
display_errors=<Off|On|stderr>Displays errors to stdout or stderr. Stderr affects only CLI and CGI binaries.
include_path=<paths separated by colon>Colon-separated paths for PHP to search for files named in include, require, include_once or require_once.
file_uploads = <Off|On>Enables/disables file uploads.
upload_max_filesize=<size>Maximum file-size for file upload; takes modifiers like M, G, etc.
max_file_uploads=<Integer>Maximum number of files that can be uploaded in a single request.
allow_url_include=<Off|On>Inclusion of PHP files from URLs. This can pose a security threat; malicious files can be included from remote servers.
extension_dir=<path>Location to find PHP extensions.
extension=<filename>Tells PHP to load the extension named. filename takes full path to the extension, else it will be sought in extension_dir.