Beginner PHP's 18 basic routines

Source: Internet
Author: User
Tags date exit config count header include php file php and
Beginner

Site original content, reproduced please indicate the source Web page teaching network .

How do we create our first PHP page? It's very simple! Choose one of the best design tools we use, and of course you can just use Notepad. After you create it, remember to save it as a file with the extension PHP and upload it to our server.

Before writing a PHP program, we usually need to configure our environment, which means that the server supports PHP.

  First, the basic structure of PHP:

Using the Include function

<Html>
<Head>
<title>your page Subject and domain name</title>

The above content for each page we use the title, do not move.

Head of each page:

<meta name= "content=" ">
"" Your others meta tag
"" Your others meta tag
"" Your others meta tag
"" Your others meta tag
"" Your others meta tag
"" Your others meta tag
"" Your others meta tag
Important JavaScripts code to put this
CSS settings put this

The above content is saved as header.php, so that each page's head is the same.

? PHP include ("header.php")?>

<body>

All the content of your page

</body>

Save As footer.php so that the bottom of each page is the same.

? Include ("footer.php");? >

Fill in our Copyright information

</body>

  Second: How to output text or connect text to a PHP

How do you display text in PHP? Use the following command:

<?php echo "Hello in PHP";? >

How do I create a connection?

<?php echo "<a href=\" http://www.webjx.com\ ">www.webjx.com.com</a>";? >

How do I create a connection with a style?

<?php echo "<font style=\" color:blue;font-size:10px;font-family:verdana;\ ">free Scripts by: <a href=\" Http://www.webjx.com.com\ "target=\" _blank\ "title=\" free Scripts by:webjx.com\ ">http://www.webjx.com</font ></a> ";? >

"echo" is used to display the output.

  Three: How to implement pagination:

If a lot of content is on your page, then we'll consider using pagination as a way to display it.

Simple page-Splitting code:

<title>webjx.com</title>
<body text= "#000000" >
<center>
<table width= "border=" 3 ">
<tr>
&LT;TD width= "140" height= "valign=" "Top" >
<p align= "center" >
<a href= ' index.php ' >home</a><br/>
<a href= ' index.php?p=page1 ' >page 1</a><br/>
<a href= ' index.php?p=page2 ' >page 2</a><br/>
</p></td>
&LT;TD width= "360" height= "valign=" "Top" >
<p>
?
Function index ()
{
echo "<p align=center>welcome to the tutorial<br/>here you can find funny
tricks</p><br/><br/><br/><br/><br/><br "; }
$choice=$_get[' P '];

Switch ($choice)
{
Case "Page1":

echo "<p align=center>page1 text, IMG and so on here</p>";
Break

Case "Page2":

echo "<p align=center>page2 text, IMG and so on here</p>";
Break

Default
Index ();
}

?>

</p>
</td>
</tr>
</table> </center>
</body>

The above files must be saved as index.php

Code for Advanced Paging:

<title>webjx.com</title>
<body text= "#000000" >
<center>
<table width= "border=" 3 ">
<tr>
&LT;TD width= "140" height= "valign=" "Top" >
<p align= "center" >
<a href= ' index.php ' >home</a><br/>
<a href= ' index.php?action=contact_us ' >page 1</a><br/>
<a href= ' index.php?action=link_us ' >page 2</a><br/>
</p></td>
&LT;TD width= "360" height= "valign=" "Top" >
<p>
?

if (Isset ($_get[' action ')) $page = $_get[' action '];

else $page = ' home ';
Switch ($page) {
1-index
Case ' home ':
Include (' incl/home.php ');
Break

2-contact form
Case ' Contact_us ':
Include (' incl/contact_us.php ');
Break
3-link US
Case ' Link_us ':
Include (' incl/link_us.php ');
Break
Default
echo ' <p align=center>error 404! The page you request doesn t exist or as been temporarely unaccessible</p> ';
Break
}

?>

</p>
</td>
</tr>
</table>
</center>
</body>

To provide a demo download, please try it yourself!

  Four: Page load time code:

<?php

Echo ("Page Took:");
$load = Microtime ();
Print (Number_format ($load,2));
Echo ("Sec to Load.");

?>

  Five: Show the code from which address to the station you are currently visiting:

<?php

echo "You Came from:<br/>";
echo $_server[' Http_referer '];
?>

  Six: Set the IP address of the turn: shielding IP

<?php

if (($remote_addr = = "22.22.22.22"))://IP Address

Print "<meta http-equiv= ' refresh ' content= ' 0; Url=http://www.sina.com ' > '/url to redicted
endif
?>

  Seven: The code for the title appears:

<title><?php include ("title.php");? ></Title>

To make a title.php file beforehand.

  Eight. How to use PHP to build an HTML table

<?php
echo "echo "echo "<title>allo</title>\n";
echo "echo "<body topmargin=\" 0\ "leftmargin=\" 0\ "rightmargin=\" 0\ "bottommargin=\" 5\ "
Bgcolor=\ "#ffffff \ >\n";
echo "<center>\n";
echo "<table width=\" 500\ "border=\" 1\ "cellspacing=\" 0\ "cellpadding=\" 0\ ">\n";
echo "<tr>\n";
echo "<td>\n";
echo "<p align=\" Center\ ">\n";
Echo
\ n ";
echo "Text Goes here\n";
echo "</p>\n";
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "</center>";
echo "</body>\n";
echo "?>

  Ix. declaring a String variable:

Create a page (config.php) Paste the following code:

?
$name= "Name";
$salutation= "hey!";
$title= "webjx.com";
$copyrights= "©2005 webjx.com";
$link_1= "Home";
?>

Create a page (test.php) to put the following code

? Include ("config.php");? >

Put it before

<p align= "center" ><? Echo ("$title");? ></p>

You can also declare this:

?
$surname1= "Marco"; $lastname1= "Charette";
?>

Call:

? Echo ("$surname1 $lastname1");? >

  Ten, display your server information:

? Phpinfo ();?>

  11: To achieve the page's jump:

Create a "jump.php" page and set a variable to hide the address of the connection

?
if ($id = = "1") {$link = "http://webjx.com";}
if ($id = = "2") {$link = "http://google.com";}

Header ("Location: $link"); Implementation of the Jump
Exit ();
?>

After saving, add the following code to another page:

<a href= "jump.php?id=1" >visit this link</a>

Jump and keep the visitor on your page:

?
if ($id = = "1") {$link = "http://webjx.com";}
if ($id = = "2") {$link = "http://google.com";}

echo "<frameset rows=\" 0,100%\ "border=\" 0\ ">\n";
echo "<frame src=\" jump.php\ "name=\" head\ "scrolling=\" no\ "marginheight=\" 0\ "frameborder=\" 0\ "noresize>\n";
echo "<frame src=\" $link\ "name=\" body\ "marginheight=\" 10\ "marginwidth=\" 10\ "frameborder=\" 0\ "noresize>\n";
echo "</frameset>\n";

Header ("Location: $link");
Exit ();
?>

Save As jump.php and then add the connection code to another page:

<a href= "jump.php?id=1" >visit this link</a>

  12, protect the page:

? $referer = getenv ("Http_referer");
if (!STRCHR ($referer, "http://webjx.com/page.php")) {
echo "<script>alert (' You can't access this page ');
window.location= ' http://webjx.com ';</script> ';
Exit (); }?>

  13: Restrict access to a page

Choose a user name and password between the "" symbol
//===========================
$admin_user_name= "admin"; Your admin username
$admin_password= "Pass"; Your password
$site_com= "webjx.com"; Your website name without http://and www
$login= "You have landed"; Succesful message as user are logged in (not necessary)
//===========================

Don't EDIT nothing below!

if ((!isset ($php_auth_user)) | | (!isset ($PHP_AUTH_PW)) {

/* No values:send Headers causing dialog box to appear * *

Header (' Www-authenticate:basic realm= "$site_com");

Header (' http/1.0 401 Unauthorized ');

Echo ' Exit

else if ((Isset ($php_auth_user)) && (Isset ($PHP_AUTH_PW)) {
/* values contain some values, so check to the if they ' re correct * *

if (($php_auth_user!= "$admin_user_name") | | ($PHP_AUTH_PW!= "$admin_password")) {/* If either the username entered is incorrect, or the password entered is incorrect, send the headers causing X to appear * *

Header (' Www-authenticate:basic realm= "$site_com");
Header (' http/1.0 401 Unauthorized ');
Echo ' Exit
else if (($php_auth_user = "$admin_user_name") | | ($PHP_AUTH_PW = = "$admin_password")) {echo "$login<br/>";
}
}

?>

Save As log.php, add the following code to the Head page, and all pages will be protected.

? Require ("log.php");? >

  14. Make an automatic turn:

<?php

Header ("refresh:0; Url=http://webjx.com ");

?>

You must save it as a PHP file.

  xv Make a list of users:

?
$customer[0] = ' WEBJX ';
$CUSTOMER[1] = ' web ';
$CUSTOMER[2] = ' Mutou ';
$CUSTOMER[3] = ' chuxia ';
$CUSTOMER[4] = ' Shenhua ';
echo "The 3rd User is: $customer[2]";

echo "<br/><br/> All users: <br/><br/>";

$number = 5;
$x = 0;
while ($x < $number) {
$customernumber = $x + 0;
echo "Costumer Name $customernumber is $customer[$x]<br/>";
++$x;
}
?>
will be displayed:
The third customer is Mutou

All Users:

Costumer Name 0 is WEBJX
Costumer Name 1 is web
Costumer Name 2 is Mutou
Costumer Name 3 is Chuxia
Costumer Name 4 is Shenhua

Another way to read an array:

?
echo "3 of my customer are: $customer[0],". "$customer[1] and". "$customer[2". "<br/>";
?>

will be displayed:
3 of my customer ARE:WEBJX, Web and Mutou.

  16. Statistics of the number of files in a directory:

?
Echo ("There is");
$dir = "/path/to/the/folder/in/your/computer";
$count = 0;
$handle=opendir ($dir);
while (($file = Readdir ($handle))!== false) {if ($file!= "." && $file!= "...") {$count++;}}
Echo $count;

Echo ("Pages for your Search in this Directory.");
?>

  17. Displays the current date or the date of the latest update:

<?php echo Date ("F d Y");?>

Show Update Date:

<?php echo "Last Modified:". Date ("m/d/y.", Getlastmod ());? >

  18. Code that repeats one character multiple times:


?
Echo str_repeat ("-", 30);
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.