PHP On-The-Fly!

Written by Dennis Pallett


Introduction PHP can be used for a lot of different things, and is one ofrepparttar most powerful scripting languages available onrepparttar 105081 web. Not to mention it's extremely cheap and widely used. However, one thing that PHP is lacking, and in fact most scripting languages are, is a way to update pages in real-time, without having to reload a page or submit a form.

The internet wasn't made for this. The web browser closesrepparttar 105082 connection withrepparttar 105083 web server as soon as it has received allrepparttar 105084 data. This means that after this no more data can be exchanged. What if you want to do an update though? If you're building a PHP application (e.g. a high-quality content management system), then it'd be ideal if it worked almost like a native Windows/Linux application.

But that requires real-time updates. Something that isn't possible, or so you would think. A good example of an application that works in (almost) real-time is Google's GMail. Everything is JavaScript powered, and it's very powerful and dynamic. In fact, this is one ofrepparttar 105085 biggest selling-points of GMail. What if you could have this in your own PHP websites as well? Guess what, I'm going to show you in this article.

How does it work? If you want to execute a PHP script, you need to reload a page, submit a form, or something similar. Basically, a new connection torepparttar 105086 server needs to be opened, and this means thatrepparttar 105087 browser goes to a new page, losingrepparttar 105088 previous page. For a long while now, web developers have been using tricks to get around this, like using a 1x1 iframe, where a new PHP page is loaded, but this is far from ideal.

Now, there is a new way of executing a PHP script without having to reloadrepparttar 105089 page. The basis behind this new way is a JavaScript component calledrepparttar 105090 XML HTTP Request Object. See http://jibbering.com/2002/4/httprequest.html for more information aboutrepparttar 105091 component. It is supported in all major browsers (Internet Explorer 5.5+, Safari, Mozilla/Firefox and Opera 7.6+).

With this object and some custom JavaScript functions, you can create some rather impressive PHP applications. Let's look at a first example, which dynamically updatesrepparttar 105092 date/time.

Example 1 First, copyrepparttar 105093 code below and save it in a file called 'script.js':

 var xmlhttp=false; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation ofrepparttar 105094 objects.  try {  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  } catch (e) {  try {  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  } catch (E) {  xmlhttp = false;  }  } @end @*/ if (!xmlhttp && typeof XMLHttpRequest!='undefined') {  xmlhttp = new XMLHttpRequest(); }

function loadFragmentInToElement(fragment_url, element_id) { var element = document.getElementById(element_id); element.innerHTML = '<em>Loading ...</em>'; xmlhttp.open("GET", fragment_url); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { element.innerHTML = xmlhttp.responseText; } } xmlhttp.send(null); }

Then copyrepparttar 105095 code below, and paste it in a file called 'server1.php':
 <?php echo date("l dS of F Y h:i:s A"); ?> 
And finally, copyrepparttar 105096 code below, and paste it in a file called 'client1.php'. Please note though that you need to editrepparttar 105097 line that says 'http://www.yourdomain.com/server1.php' torepparttar 105098 correct location of server1.php on your server.
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"> <html> <head> <title>Example 1</title> <script src="script.js" type="text/javascript"></script>

<script type="text/javascript"> function updatedate() { loadFragmentInToElement('http://www.yourdomain.com/server1.php', 'currentdate'); }

</script> </head>

<body> The current date is<span id="currentdate"><?php echo date("l dS of F Y h:i:s A"); ?></span>.<br /><br />

<input type="button" value="Update date" OnClick="updatedate();" /> </body>

PHP and Cookies; a good mix!

Written by Dennis Pallett


Introduction Cookies have long been used in PHP scripts, and are a very useful function. But what exactly are cookies? Maybe you have used then, but you still don't know exactly what they are. Or you are completely new to cookies? It doesn't matter, because in this tutorial I will show you exactly what cookies are, and what they are used for.

Cookies in a nutshell Cookies are small pieces of information that is stored onrepparttar computer of your visitors. Each browser handles it differently, but most simply storerepparttar 105079 information in a small text file. Internet Explorer has a special folder, which can be found in your C:Windows or C:WindowsSystem32 folder. You can delete all your cookies, by going torepparttar 105080 Options and 'Clearing Cookies' or deleting them by hand. I don't recommend this though.

Almost every website uses cookies. If you go to Amazon.com, you will get several cookies. The same goes for CNN.com. Even Google uses cookies! They are extremely useful for (temporarily) storing information. For example, if you have a login system for your visitors, you could save their userid and password (very heavily encrypted!) so they are automatically logged inrepparttar 105081 next time they visit your website.

Or you could remember their last visit, and highlight everything that is new. And that's justrepparttar 105082 beginning.

Using Cookies Using cookies in PHP is extremely easy. In fact, there is nothing to it, because of PHP's inbuilt setcookie() function. Have a look atrepparttar 105083 documentation, and then tryrepparttar 105084 following example:

<?php

// Set a cookie // Cookie name: name // Cookie value: Dennis Pallett // Cookie expire: in 24 hours

setcookie ('name', 'Dennis Pallett', time() + (60*60*24)); ?>

If you runrepparttar 105085 code above, then a cookie will be set. That's all. The cookie name and value are pretty obvious. The cookie expire is whenrepparttar 105086 cookie expires, or goes away. Simply userepparttar 105087 time() function and addrepparttar 105088 number of seconds you want to haverepparttar 105089 cookie available to it. Inrepparttar 105090 example I added 60*60*24=86400 seconds, or 24 hours.

If you have looked atrepparttar 105091 documentation, you probably noticed there are additional arguments. Asrepparttar 105092 documentation says,repparttar 105093 path is to limit a cookie to a specific path on your web server. This is often used when you run multiple instances ofrepparttar 105094 same script in separate directories. You can safely omit this argument when it doesn't matter ifrepparttar 105095 cookie is available site-wide.

There is alsorepparttar 105096 domain argument. This can be used to limitrepparttar 105097 cookie to a specific sub-domain, e.g. test.example.com. You can also safely ignore this argument, or set it to .example.com (noterepparttar 105098 beginning period, this is essential!).

Finally, there is alsorepparttar 105099 secure argument. This argument is only used for cookies that are sent over a secure HTTPS connection (SSL). Just ignore this argument, unless you're working with a secure connection.

One thing that should be mentioned is that cookies must be set, before you display any HTML/text. It's probably best if you turn on output buffering by putting ob_start() atrepparttar 105100 top of your page.

Now that you have set a cookie, you probably want to retrieverepparttar 105101 value as well. After all, that isrepparttar 105102 whole point of using cookies. Thankfully, as PHP is ever so easy, you can retrieverepparttar 105103 same way as you retrieve a GET value. Seerepparttar 105104 following example to retrieverepparttar 105105 value ofrepparttar 105106 previous example:

<?php echo 'Your name is ' . $_COOKIE['name']; ?>

This should print "Your name is Dennis Pallett". There's nothing more to it. It's just that easy!

Finally, one thing you probably want to do as well is remove cookies. This is as easy as setting them. Simply changerepparttar 105107 value ofrepparttar 105108 cookie to FALSE, and changerepparttar 105109 expire date to -3000 seconds. Seerepparttar 105110 following example:

Cont'd on page 2 ==>
 
ImproveHomeLife.com © 2005
Terms of Use