Php Lesson 3:- Php Function and Cookies
(335 reads)
Using PHP Information
In Php the following phpinfo() function is used to output PHP information.
This function is useful for trouble shooting, providing the version of PHP,
and how it is configured.
The phpinfo() function options
Name Description
INFO_GENERAL The configuration line, php.ini location, build
date, Web Server, System and more
INFO_CREDITS PHP 4 credits
INFO_CONFIGURATION Local and master values for php directives
INFO_MODULES Loaded modules
INFO_ENVIRONMENT Environment variable information
INFO_VARIABLES All predefined variables from EGPCS (Environment,
GET, POST, Cookie, Server)
INFO_LICENSE PHP license information
INFO_ALL Shows all of the above. This is the default value
Example
<html>
<body><?php
// Show all PHP information
phpinfo();
?><?php
// Show only the general information
phpinfo(INFO_GENERAL);
?></body>
</html>
Using PHP Server Variables
All servers hold information such as which URL the user came from, what's the
user's browser, and other information. This information is stored in variables.
In PHP, the $_SERVER is a reserved variable that contains all server information.
The $_SERVER is a global variable - which means that it's available in all scopes
of a PHP script.
Example
The following example will output which URL the user came from, the user's browser,
and the user's IP address:
<html>
<body><?php
echo "Referer: " . $_SERVER["HTTP_REFERER"] . "<br
/>";
echo "Browser: " . $_SERVER["HTTP_USER_AGENT"] . "<br
/>";
echo "User's IP address: " . $_SERVER["REMOTE_ADDR"];
?></body>
</html>
Using PHP Header() Function
The header() function is used to send raw HTTP headers over the HTTP protocol.
Note: This function must be called before anything is written to the page!
Example
The following example will redirect the browser to the following URL: http://compbuy.co.uk/webdesign/:
<?php
//Redirect browser
header("Location:http://compbuy.co.uk/webdesign/");
?><html>
<body>......</body>
</html>
Note: This function also takes a second parameter - an optional value of true
or false to determine if the header should replace the previous header. Default
is TRUE.
However, if you pass in FALSE as the second argument you can FORCE multiple
headers of the same type.
Example
<?php
header("WWW-Authenticate: Negotiate");
header("WWW-Authenticate: NTLM", FALSE);
?><html>
<body>......</body>
</html>
PHP Form Handling
The most important thing to notice when dealing with HTML forms and PHP is that
any form element in an HTML page will automatically be available to your PHP
scripts.
Look at the following example of an HTML form:
<html>
<body><form action="welcome.php" method="POST">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form></body>
</html>
The example HTML page above contains two input fields and a submit button.
When the user fills in this form and hits the submit button, the "welcome.php"
file is called.
The "welcome.php" file looks like this:
<html>
<body>Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!</body>
</html>
A sample output of the above script may be:
Welcome John.
You are 28 years old!
Here is how it works: The $_POST["name"] and $_POST["age"]
variables are automatically set for you by PHP. The $_POST contains all POST
data.
Note: If the method attribute of the form is GET, then the form information
will be set in $_GET instead of $_POST.
How to Create a Cookie using Php
In Php the setcookie() function is used to create cookies.
Note: The setcookie() function must appear BEFORE the <html> tag.
Syntax
setcookie(name, value, expire, path, domain);
Example
The following example sets a cookie named "uname" - that expires after
ten hours.
<?php
setcookie("uname", $name, time()+36000);
?><html>
<body><p>
A cookie was set on this page! The cookie will be active when
the client has sent the cookie back to the server.
</p></body>
</html>
How to Retrieve a Cookie Value
When a cookie is set, PHP uses the cookie name as a variable.
To access a cookie you just refer to the cookie name as a variable.
Tip: Use the isset() function to find out if a cookie has been set.
Example
The following example tests if the uname cookie has been set, and prints an
appropriate message.
<html>
<body><?php
if (isset($_COOKIE["uname"]))
echo "Welcome " . $_COOKIE["uname"] . "!<br />";
else
echo "You are not logged in!<br />";
?></body>
</html>
|