PHP - Basic architecture - One for one

Here we will describe "One on one" PHP application architecture.

In this architecture, you have one file for every function you have in your application. Every file contains code to perform only one function and displays results.

Example is same as in "One for all" architecture, but notice the difference in the link code. Also, notice the link in the address bar when you click on link. In "One for all" scripts are executed in context of index.php (address is index.php?cmd=displayserverinfo). In this example, they are executed in their own context (address is cmd1.php). We call the scripts directly, we do not include them.
This approach is good because you can better organize your code (in more files), the code is more "replaceable", but you may have a problem with common functions. For example, if you have a function you need to call in all your scripts (usually security functions, or functions to start displaying a html page), you need to call them in all of these files.

##### begin #####
##### index.php #####
<html>
<head>
<title>One on one</title>
</head>
<body>
<h2>Hello, click on a link bellow</h2>
<p><a href="cmd1.php">Display Server info</a></p>
<p><a href="cmd2.php">Display Browser info</a></p>
<p><a href="cmd3.php">Display PHP info</a></p>
</body>
</html>
##### index.php #####
##### end #####
##### begin #####
##### cmd1.php #####
<html>
<head>
<title>One on one</title>
</head>
<body>
<?php
  echo "<pre>";
  echo $_SERVER["SERVER_SIGNATURE"] . "\n";
  echo $_SERVER["SERVER_SOFTWARE"] . "\n";
  echo $_SERVER["SERVER_NAME"] . "\n";
  echo $_SERVER["SERVER_ADDR"] . "\n";
  echo $_SERVER["SERVER_PORT"] . "\n";
  echo "</pre>";
?>
</body>
</html>
##### cmd1.php #####
##### end #####
##### begin #####
##### cmd2.php #####
<html>
<head>
<title>One on one</title>
</head>
<body>
<?php
  echo "<pre>";
  echo "USER AGENT: " . $_SERVER["HTTP_USER_AGENT"] . "\n";
  echo "ACCEPTS FILES OF TYPE: " . $_SERVER["HTTP_ACCEPT"] . "\n";
  echo "ACCEPTS LANGUAGE: " . $_SERVER["HTTP_ACCEPT_LANGUAGE"] . "\n";
  echo "REFERER: " . $_SERVER["HTTP_REFERER"] . "\n";
  echo "COOKIE: " . $_SERVER["HTTP_COOKIE"] . "\n";
  echo "</pre>";
?>
</body>
</html>
##### cmd2.php #####
##### end #####
##### begin #####
##### cmd3.php #####
<?php
  phpinfo();
?>
##### cmd3.php #####
##### end #####
 

Related Marakana Courses

  • PHP and MySQL Bootcamp Training

Leave a Reply