Cookies and PHP, Mail()
Another great feature in PHP is cookie handling, PHP lets us do this with ease. The function we use to set a cooke is setcookie().
It's as easy as that, the first parameter is the string name of the cookie, this cookie will now be know as username. The second value is the value which you want to store in the cookie, in this case it's a variable $name. The above is the bear minimum, this is the simplest cookie you can set, and this cookie will deleted when the user ends their session (closes their browser). We can define how long we wish a cookie to last:
It's as easy as that. Another function on PHP is time(). This simply returns the current UNIX timestamp, so we call this, and then add 31536000 seconds to it. What is 31536000 seconds? That's right, a year, so this cookie will last for one whole year. That is if the client doesn't delete it or something, anyways, the third parameter is the life of the cookie, how long we want it to last, if this is left undeclared the cookie is deleted when the user's session ends. If we wish to delete a user's cookie we can simply set the life to a time in the past, e.g.: time() - 3600. That will set the user's cookie life to one hour in the past, deleting it. Remember, cookies must be deleted with the same parameters they were set with.
We can also use a forth parameter, the fourth parameter is the cookie path.
The fourth parameter defines a path from which the cookie can be accessed. In the above example only scripts inside /jesteruk/folder/ will be able to access the cookie ($username), in scripts outside this folder the cookie will be unavailable.
Yup, there's a fifth parameter, the fifth parameter defines a hostname that can retrieve the cookie.
The fifth parameter is the domain which can retrieve this cookie. Normally a cookie can only be accessed by the domain that set it, otherwise sites would be able to get private information from cookies set by other sites. With this parameter we can define another domain which can retrieve the cookie we are setting.
Retrieving the Cookie
Once you have set a cookie on a user's system, it is only available when they load the next page. The cookie can be accessed very easily:
When the cookie is set the value is automatically URL encoded, when the script retrieves a cookie, it automatically decodes the value, so you don't have to worry about that.
Always remember, a limitation of cookies is that you have to set it before you can send the HTTP headers. In other words, always do all your cookie setting BEFORE you output any HTML, otherwise the cookie will not be set and your page will have an ugly error stuck on it.
Another thing made easy in PHP is sending mail. We use the mail() function.
Simple eh? The first parameter is the address to which the email is destined. The second is the subject of the email. The third parameter is the actual message. This is the minimal use of this function. By using a fourth parameter we can add additional headers to our email.
This allows us to define raw headers to be sent with the E-Mail, such as an address for the receiver to reply to, an address so the receiver can see who the E-Mail was from. The headers should be separated with rn
Sending mail in this way makes it very easy, we don't have to actually "talk" to the sendmail program as we would if we were using Perl, although this can still be done is desired. We can use string building techniques to build an E-Mail message:
By using the .= operator we're telling PHP to append the text in the quotes onto the end of the data already stored in $message, allowing us to build up a formatted message. We can also send HTML E-Mail:
The Content-type header allows us to send the E-Mail as HTML, so we can include tables and format the E-Mail as we would a HTML page. The default is plain text, so if you want to send it as HTML you have to include the header.
No Comments for this page.