ID #1432

Tips and Tricks

Hello World

First of all you will learnhow to display a normal text with PHP in a web WWW- und FTP-Servern im Internet abzurufen. Bekannte Browser sind Netscape Navigator, Microsoft Internet Explorer, Opera und Mosaic.">browser.
A PHP file may contain both HTML and PHP instructions.
Create a file called "hello.php" and enter the following into it...

<?php
echo "Hello World";
?>

Save the file and call it up with the web WWW- und FTP-Servern im Internet abzurufen. Bekannte Browser sind Netscape Navigator, Microsoft Internet Explorer, Opera und Mosaic.">browser afterwards (http://localhost/hello.php).
You can see just Hello World in the WWW- und FTP-Servern im Internet abzurufen. Bekannte Browser sind Netscape Navigator, Microsoft Internet Explorer, Opera und Mosaic.">browser.

Explanation:

<?php

The arrow bracket, the question mark and "php" are always used when utilizing PHP.

echo "Hello World";

With "echo" a certain text is outputted. The text stands within two quotation marks. The semicolon (;) ends the function "echo".

With the concluding...

?>

...the PHP block is completed.


Variables

Variables are used to store text or numbers. They can - in return - be outputted by means of "echo".

Variables always begin with a Dollar sign ($), then the variable's name and finally a (=). Then follows the text (in quotation marks) and finally the semicolon (;).
You may name the variable whatever you want, however you have to consider case sensitivity.

Example:

<?php
$name = "Franz Müller";
echo "My name is $name";
?>

The ouput of the web WWW- und FTP-Servern im Internet abzurufen. Bekannte Browser sind Netscape Navigator, Microsoft Internet Explorer, Opera und Mosaic.">browser is:

My name ist Franz Müller


if-instruction

With "if" you can check if something has been done (has a certain condition been fulfilled?).

An if-instruction could look like this:

<?php
if(condition)
{
instruction
}
?>

The condition that ought to be checked is set in brackets after the initial "if". The instruction is following in curly brackets. It is executed if the condition is true.

An example:

<?php
$name = "Franz";

if($name == "Franz")
{
echo "Hello Franz";
}
?>

"if" checks, if the variable "$name" has the content "Franz". If that's the case "if" returns true and we get the output Hello Franz.

Please note that you have to use two equals signs ("==").

If the variable "$name" doesn't have the content Franz, "if" returns false and you get no output.

To get an output after a false condition we need the else-instruction.

It looks like this:

<?php
if(condition)
{
instruction
}
else
{
instruction
}
?>

"else" always occurs, when a false is returned during a check.

An example:

<?php
$name = "Franz";

if($name == "Franz")
{
echo "Hello Franz";
}
else
{
echo "You aren't Franz";
}
?>

If the variable "$name" isn't Franz, you will get the output "You aren't Franz" in the web WWW- und FTP-Servern im Internet abzurufen. Bekannte Browser sind Netscape Navigator, Microsoft Internet Explorer, Opera und Mosaic.">browser.


Password protection

The HTTP authentication by PHP is only available if PHP is running as Apache module and doesn't work with the CGI version for this reason.

In a PHP script for an Apache module you can use the function header() to send the message "Authentication required" to the client server so he can open a window for entering the password/username. When the user has entered his data the PHP script is called up again with the variables $PHP_AUTH_USER and $PHP_AUTH_PW, containing the respective username and the password.

Variant 1:
After having entered the correct username and password the contained HTML code will be displayed.

<?php
if ($PHP_AUTH_USER!="test" OR $PHP_AUTH_PW!="test") // username and password
{
Header('HTTP/1.1 401 Unauthorized');
Header('WWW-Authenticate: Basic realm="Top Secret"');

echo "Error 401 // Unauthorized.\n"; // Text will be displayed when the user clicks // "Cancel"

exit;
}
?>

<html>
<head>
<title></title>
</head>
<body>

protected content

</body>
</html>


Variant 2:
The user is lead to believe in accessing a protected file or URL. He will be redirected to it after entering the right username and password. Please note that this is not an actual password protection!

<?php
if($PHP_AUTH_USER!="test" OR $PHP_AUTH_PW!="test") // username and password
{
Header('HTTP/1.1 401 Unauthorized');
Header('WWW-Authenticate: Basic realm="Top Secret"');

echo "Error 401 // Unauthorized.\n"; // Text will be displayed when the user clicks // "Cancel"
exit;
}

header("Location: http://www.myaddress.com");
?>


Variant 3:
The user is lead to believe in accessing a protected file or URL. After entering the correc tusername and password the file is being loaded into a full-screen frame. Please note that this is not an actual password protection and that the file/URL can be accessed directly if it's not protected itself (see variant 1).

<?php
if($PHP_AUTH_USER!="test" OR $PHP_AUTH_PW!="test") // username and password
{
Header('HTTP/1.1 401 Unauthorized');
Header('WWW-Authenticate: Basic realm="Top Secret"');

echo "Error 401 // Unauthorized.\n"; // Text will be displayed when the user clicks // "Cancel"
exit;
}
?>

<html>
<head>
<title></title>
</head>

<frameset rows="100%"" frameborder="NO" border="0" framespacing="0">

<frame src="http://www.myaddress.com" name="mainFrame">
</frameset>

<noframes><body>
</body></noframes>
</html>

Tags: php, selfphp, webdesign

Verwandte Artikel:

Kommentieren nicht möglich