On the syntax of PHP (IV.)

Source: Internet
Author: User
Tags empty header mail php script setcookie variable client
Grammar ("Talking about PHP grammar (c)") at the end of the last mention of cookies and session, this article on the two technologies to introduce some.
Cookies are often read as "Memory", also called "small dessert". It is a small file stored in the client's browser. It is developed to address a single connection to HTTP without Queensland memory, and can be used to track the user or confirm the users who have returned. PHP provides a setcookie () function that allows you to set cookies. Because cookies are part of the Queensland HTTP header. Therefore, the Setcookie () function must be invoked before the Web page data is passed to the browser. This is the same as calling the header () function.
Cookies must be provided by the host side, so we have to send out a header that has a cookie set in the CGI program. The following is a Queensland example of invoking the Setcookie () function setting cookie in PHP:

<?php
Setcookie ("User", "Wind", Time () +3600, "/php/", "http://www.oso.com.cn");
?>

Where user is the name of the cookie, the value for which wind is the cookie, time () +3600 is the valid duration of the cookie,/php/is the relevant path to the cookie, and http://www. Queensland oso.com.cn the Web site for the cookie.
In fact, in addition to using this way to set cookies, we can also use the header () function such as: Header ("Set-cookie:user=wind"), but this to the HTTP head letter Queensland have knowledge of the line, so I do not recommend the use of this method, Or the use of Setcookie () more convenient.
When a cookie is read, the browser automatically checks to see if there is a cookie for the station, and some of the words will be automatically sent to the server, and in PHP, the returned CO Queensland Okie as a variable. If the cookie set above is returned, a $user variable will be formed, with the value of wind.
However, the cookie has a fatal disadvantage, that is, if the customer turned off the cookie to receive, unable to store cookies to the client, all operations will be wrong. As a result, S Queensland ession are provided in PHP4 to replace cookies.
The biggest difference between a session and a cookie is that the cookie saves the information to the client and the session is stored on the server side. In fact, the session is the PHP script provides a global Queensland variable. Examples are as follows:
Set a session, named user, with the value wind

<?php
Session_Start ();
$user = "Wind";
Session_register ("user");
?>

Read the session, show the result as "welcome you!" Wind
?br> <?php
Session_Start ();
echo "Welcome you!" $user ";
?>
This article is just a brief discussion of cookies and session technology, for those who want to understand the technology, please refer to other books.

Send email online, you can use the mail () function. The format is as follows:

Mail (addressee, subject, content, mail file header);

On the Internet, send an email to the webmaster, usually like this:

<a href= "mailto:webmaster@163.com" >webmaster@163.com</a>

In this way, when clicked on webmaster@163.com this link, will launch the default mail editor to write the email, is really troublesome, uses the mail () function, then can easily make up an online letter-sending page. The recipients of this kind of page are fixed (webmaster@163.com), have to fill in the address of the recipient, and the subject we can also set it down. The following example:

File: email.html
<title> Letters to network management </title>
<body>
&LT;H2 align= "Center" > Network Management Receipt <center>
<form action= "mail.php" >
Sender: <input type= "text" Name= "from" size=25><br>
Topic: <input type= "text" name= "subject" size=20><br>
Content:
<textarea name= "Content" cols=80 rows=15> Hello, webmaster:</textarea><br>
<input type= "Submit" value= "send out" > <input type= "reset" value= "rewrite" ><br>
</form>
</body>
File: mail.php

<?php
if (empty ($from) or empty ($subject) or empty ($content)) {echo is not completed, please <a href= "email.html" > Return </a>;}
$body = "[subject] $subjectn";
$body. = "[sender] $fromn";
$body. = $content;
$deal =mail ("webmaster@163.com", $subject, $body, "from: $from");
if ($deal) {echo sent successfully! ";} Else{echo "Send a piece of failure!!! ";}
?>

PHP introduced to this, and almost, you are not feeling the powerful PHP, is not also want to join the ranks of PHP, right now. To make yourself fly in PHP, this knowledge is still Queensland, it can only help you get started. After getting started, the practice is personal. My friend, Goodbye--(^_^)


< finished full >


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.