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


Consider my url string is :

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

Output :


Output : /products/booking.php

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

Output :

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.

$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


More examples 
echo parse_url('', PHP_URL_QUERY);
Output : 'mode=edit&id=34'
echo parse_url('', PHP_URL_FRAGMENT)
Output : 'user'
echo parse_url('', PHP_URL_SCHEME);
Output : 'http'
echo parse_url('', PHP_URL_HOST);
Output : ''
echo parse_url('', 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

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.