Archive for the ‘PHP’ Category

PHP

Well before we proceed learning php, i want you to install SERVER at your PCs. So that you can test the code locally every time you learn something new.

What is Web Server?

A web server is a computer program that delivers (serves) content, such as web pages, using the Hypertext Transfer Protocol (HTTP), over the World Wide Web. The term web server can also refer to the computer or virtual machine running the program. In large commercial deployments, a server computer running a web server can be rack-mounted with other servers to operate a web farm.

The Web server that we will be using is WAMP, Installation demonstration can easily be learn and done by seeing the video.

Steps before Installation:

Before you proceed to see installation video, it is essential that you download the following WAMP Server File.

Download WAMP

PHP

What is PHP?

PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. As a general-purpose programming language, PHP code is processed by an interpreter application in command-line mode performing desired operating system operations and producing program output on its standard output channel. It may also function as a graphical application. PHP is available as a processor for most modern web servers and as standalone interpreter on most operating systems and computing platforms.

Why PHP?

  • PHP is Open Source
  • PHP runs on different platforms like Windows, Linux, Unix, etc (Platform Independent Language)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP takes the concept of C and brings them into web surface
  • Why PHP is important to Know ?

Gradually as we can see there is an increase in technology and its usage and if we look around our world we see that there is a drastic change from what we were around 5 – 10 years to now. Almost everyday most of us are habitual of visiting Social Networks like Facebook, Twitter etc for different reasons. If we see these platforms, as a technologist, this thought must come in your mind on which platforms, languages these systems are made?

The Answer is simply : PHP – All the above mentioned systems are based on php language utilizing it in one way or the other.

What should you know ?

Before you start programming using PHP. It is important that you should be aware of the following concepts:

  • HTML / XHTML
  • Web Server
  • C Language – Would be an additional benefit
  • Example of PHP Based Systems

I would recommend everyone to please look at the example system, these systems are all based on PHP Like Facebook, WordPress etc.

PHP

At the start of my career i tried to put up some videos for helping people learn PHP. Today i thought to bring them live for all of my ideal lab readers.

The tutorial includes:

Level 1 : Basics of PHP
Task 1 : PHP Introduction
Task 2 : WAMP Installation
Task 3 : PHP Syntax
Task 4 : PHP Variables
Task 5 : PHP Strings
Task 6 : PHP Operators
Task 7 : PHP Expressions

Level 2 : Flow Control
Task 1 : PHP Conditions
Task 2 : PHP Loops
Task 3 : PHP Arrays

Level 3 : Functions
Task 1 : Intoductions to Function
Task 2 : Writing your own Function

Level 4 : PHP Forms
Task 1 : PHP Forms
Task 2 : PHP $_GET
Task 3 : PHP $_POST

 

PHP

Few days back we had a requirement on how to detect iPhone and iPad so that we can divert the traffic to a different location. Here is a quick SERVER side code in PHP which can achieve this:

<?php
$isiPad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
$isiPhone = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPhone');
if($isiPad == true || $isiPhone == true) {
	echo "Ipad - Iphone";
}
?>

You can test out the code by visiting: Link on your iPad or iPhone

Experiments Iphone PHP
string uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] )

Generate a unique ID, this  is available in version 4 and 5 of PHP.

Example Code from Reference Site:

<?php

/* A uniqid, like: 4b3403665fea6 */
printf("uniqid(): %s\r\n", uniqid());

/* We can also prefix the uniqid, this the same as
 * doing:
 *
 * $uniqid = $prefix . uniqid();
 * $uniqid = uniqid($prefix);
 */
printf("uniqid('php_'): %s\r\n", uniqid('php_'));

/* We can also activate the more_entropy parameter,
 * which is required on some systems, like Cygwin.
 * This makes uniqid()produce a value
 * like: 4b340550242239.64159797
 */
printf("uniqid('', true): %s\r\n", uniqid('', true));
?>

To read more about it visit Reference PHP Site

PHP

Looking to test whether a site link exist or not, use the following function:

function urlExists($url=NULL)
{
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode return true;
} else {
return false;
}
}
PHP

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. PDO provides a data-access abstraction layer, which means that, regardless of which database you’re using, you use the same functions to issue queries and fetch data.

PHP

Detection of browsers via php isnt hard, simply use the user-agent information, like:

function using_ie()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = False;
if(preg_match('/MSIE/i',$u_agent))
{
$ub = True;
}
return $ub;
}

Also just to add if incase you want to test for IE6 simply replace MSIE with MSIE 6.0 and it should work fine as well.

PHP

Simple Logic:

<?php
$str ="shahzaib@theideallab.com";
$s = explode("@",$str);
print end($t); //output shahzaib
?>
PHP