codescracker


php

PHP Web Developments



« Previous Tutorial Next Tutorial »


PHP is a popular programming language and plays a huge role in web development.

You can dynamically develop your web pages or even whole websites using PHP.

PHP supports more variety of databases. It means you can use more databases to connect and use in your website with the help of PHP. In all those databases, MySQL is most popular database used with PHP in web developments.

Why PHP in Web Developments

If you want to develop your web pages or websites dynamically, or database driven, then you can go for PHP.

PHP helps web developer to develop dynamic and database driven websites.

Benefits of PHP in Web Developments

There are much more benefits of using PHP in web developments. Here are the list of most common uses of PHP in web developments.

You can use PHP to

How PHP can be used in Web Developments

In these days, the uses of PHP in developing websites increases day by day.

Today, almost every web developer wants to develop database-driven website just to make his/her website more attractive to users point of view.

Here is an example shows how PHP can be used in developing web pages and web sites.

First create a file named codescracker.php and place the following code inside it, and then save that file inside the directory C:\xampp\htdocs\.

<!DOCTYPE html>
<html>
<head>
	<title>About my current position</title>
</head>
<body>
<?php
	echo "<h2>My Current Position</h2>";
	include("dynamic.php");
?>
</body>
</html>

As you can see from the above PHP code, dynamic.php is another PHP file contains some PHP code and content inside that file, you can call this file as dynamic file.

Dynamic file means when you change the content inside it, that is, dynamic.php, then the whole output produced by the file codescracker.php will also be changed .

The dynamic.php file contains the following content inside it.

<?php 
	echo "<p>I am reading the technical topics in PHP.</p>";
	echo "<p>Right now, i am learning about web developments
		using PHP.</p>";
	echo "<p>I love reading PHP right now as it is the language
		that i was looking for to increase my knowledge about 
		developing websites and webpages dynamically and 
		sometime database-driven.</p>";
	echo "<p>I am planning to design a 
		public forum of questions and answers, where anyone can 
		ask their required question and answered by the public.</p>";
	echo "<p>And without PHP, i think it can not be possible to 
		develop question and answer forum.</p>";
?>

After creating dynamic.php file and putting the above content inside it, run the above file, named codescracker.php. You will see the following output.

php web developments

Now if you change the content of file dynamic.php, then the whole output of codescracker.php will also be changed. Now put the following content inside dynamic.php and save it.

<?php 
	echo "<p>Now i am feeling something better than previous
		after learning about web developments using PHP</P>";
	echo "<p>Now i am going to learn next topics of PHP.</p>";
	echo "<p>I will learn each and every topics of PHP here.</p>";
?>

After changing the content of file dynamic.php, refresh the page or type localhost/codescracker.php again in the browser, you will see the following output which is not as of previous one.

web developments using PHP

This is just a simple example that shows how to use PHP in web developments.

You will learn a lot more topics later to develop dynamic websites or database-driven websites using PHP.

How to Identify Browser Name and Version using PHP

You can find or identify the name of your browser along with its version using PHP. Here is an example that shows how to identify browser name and browser version using PHP.

<!DOCTYPE html>
<html>
<head>
	<title>How to identify browser with version using PHP ?</title>
	<style>
		table th, td{padding:5px;border:1px solid black;}
	</style>
</head>
<body>
<h2>My Browser Name and Version</h2>
<?php
	function myBrowserName()
	{
		$userAgt = $_SERVER['HTTP_USER_AGENT'];
		$myBrowserNm = 'Unknown';
		$myBrowserVersion= "";
		if(preg_match('/MSIE/i',$userAgt) && !preg_match('/Opera/i',$userAgt))
		{
			$myBrowserNm = 'Internet Explorer';
			$ub = "MSIE";
		}
		elseif(preg_match('/Firefox/i',$userAgt))
		{
			$myBrowserNm = 'Mozilla Firefox';
			$ub = "Firefox";
		}	
		elseif(preg_match('/Chrome/i',$userAgt))
		{
			$myBrowserNm = 'Google Chrome';
			$ub = "Chrome";
		}	
		elseif(preg_match('/Safari/i',$userAgt))
		{
			$myBrowserNm = 'Apple Safari';
			$ub = "Safari";
		}
		elseif(preg_match('/Opera/i',$userAgt))
		{
			$myBrowserNm = 'Opera';
			$ub = "Opera";
		}	
		elseif(preg_match('/Netscape/i',$userAgt))
		{
			$myBrowserNm = 'Netscape';
			$ub = "Netscape";
		}
		$known = array('Version', $ub, 'other');
		$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
		if(!preg_match_all($pattern, $userAgt, $matches))
		{
		}
		$i = count($matches['browser']);
		if($i != 1)
		{
			if(strripos($userAgt,"Version") < strripos($userAgt,$ub))
			{
				$myBrowserVersion= $matches['version'][0];
			}	
			else
			{
				$myBrowserVersion= $matches['version'][1];
			}	
		}
		else
		{
			$myBrowserVersion= $matches['version'][0];
		}
		if($myBrowserVersion==null || $myBrowserVersion=="") {$myBrowserVersion="?";}
		return array
		(
			'userAgent' => $userAgt,
			'name'      => $myBrowserNm,
			'version'   => $myBrowserVersion,
			'pattern'    => $pattern 
		);
	}
	$ua=myBrowserName();
	echo "<table>";
	echo "<tr>";
		echo "<th>Browser Name</th>";
		echo "<th>Browser Version</th>";
	echo "</tr>";
	echo "<tr>";
		echo "<td>".$ua['name']."</td>";
		echo "<td>".$ua['version']."</td>";
	echo "</tr>";
	echo "</table>";
?>
</body>
</html>

Here is the sample output produced by the above browser identification example code using PHP.

php identify browser name version

You are seeing the above output, as i am using the 5.1.7th version of Safari browser.

How to Identify Platform/OS using PHP

You can also use PHP, to find or identify what platform or operating system you are using right now. Here is an example shows how to identify operating system or platform using PHP code.

<!DOCTYPE html>
<html>
<head>
	<title>How to identify Platform/OS using PHP ?</title>
</head>
<body>
<h2>My Platform/OS Name</h2>
<?php
	function myPlatformName()
	{
		$userAgt = $_SERVER['HTTP_USER_AGENT'];
		$platformNm = 'Unknown';
		if(preg_match('/linux/i', $userAgt))
		{
			$platformNm = 'linux';
		}	
		elseif(preg_match('/macintosh|mac os x/i', $userAgt))
		{
			$platformNm = 'mac';
		}	
		elseif(preg_match('/windows|win32/i', $userAgt))
		{
			$platformNm = 'windows';
		}
		return array
		(
			'platform'  => $platformNm
		);
	}
	$ua=myPlatformName();
	echo "My Platform/OS Name = ".ucfirst($ua['platform']);
?>
</body>
</html>

The above example code on identifying platform or operating system using PHP will produce the following output:

php identify platform

As the above program run on windows operating system, therefore the output produced is Windows.

Browser Redirection Code in PHP

You can create browser redirection code using PHP. Browser redirection comes in many form. Browser redirection means your will be redirected to another web page.

Here is a simple example of browser redirection using PHP. In this example, instead of redirecting to another web page in the same browser window, you will be redirected to another web page but in another browser window.

<?php
	// below code will check, if user submitted 
	// the data or not
	if(isset($_POST["redirectURL"]))
	{
		// below code will get the location url
		$location = $_POST["redirectURL"];
		// below code will redirect user to 
		// the choosen url present in $location variable
		header("Location: $location");
		// below code will skip executing the below code 
		// after it
		exit();
	}
?>
<html>
<head>
	<title>Browser Redirection Code in PHP</title>
</head>
<body>
	<p>What you like to learn ?</p>
	<form method="POST" target="_blank">
	<select name="redirectURL">
	<option value="">Choose to Learn</option>
	<option value="https://codescracker.com/html">
		HTML Tutorial
	</option>
	<option value="https://codescracker.com/java">
		Java Tutorial
	</option>
	<option value="https://codescracker.com/c">
		C Tutorial
	</option>
	<option value="https://codescracker.com/cpp">
		C++ Tutorial
	</option>
	<option value="https://codescracker.com/js">
		JavaScript Tutorial
	</option>
	<option value="https://codescracker.com/sql">
		SQL Tutorial
	</option>
	</select>
	<input type="submit" />
	</form>
</body>
</html>

Here is the sample output produced by the above browser redirection example code in PHP.

php browser redirection

Now to check how you will be redirected using browser redirection in PHP, just select any tutorial from the drop-down menu say C++ Tutorial and click on the Submit button, then you will be redirected to that page in another window as shown in the figure given below, this is the page you will see after selecting C++ and clicking on the Submit button.

browser redirection example in php

« Previous Tutorial Next Tutorial »


Latest 5 Posts by CodesCracker



Tools
Calculator

Quick Links
Signup - Login - Give Online Test