Functional Differences Between CGI, JavaScript, and PHP

0 comments | 277 views

Scripts can change web pages and web page behavior.

Some scripts change the web page source code on the server before the page is sent to the browser. Some scripts change the web page after it’s already loaded in the browser.

Understanding when and where different types of scripts run may explain why certain things don’t work the way it may seem they should.

CGI script files are separate from web page files. The scripts run on the server. Anything that’s done to the web page source code and content is done before the browser sees the page.

The final form of web page content created or modified by CGI scripts is available to search engine spiders just like static content is.

JavaScript code, on the other hand, is integral with the web page source code. It runs within the browser itself.

Content created or modified with JavaScript is available to search engine spiders that parse and run JavaScript. Whether or not they do is something I don’t know. It is likely to depend on which search engine’s spider is doing the crawling. (I do know Googlebot grabs URLs from within JavaScript code and follows them under certain conditions.)

And then there’s PHP.

PHP scripts can be within web page source code files. But the code doesn’t run in the browser. PHP scripts run on the server, before the browser receives the source code.

That can cause some confusion. If the script is in the web page source code file, then it may seem like it should run in the browser. But it doesn’t.

If your server is in a different time zone than your browser and you put a command to display the time both with PHP code and with JavaScript code, the two time displays will be different. The PHP will cause the display of the time on the server and the JavaScript will display the time on your computer.

To test, put this on a temporary web page named something like temp.php, upload it to your server. Then type it’s URL into your browser.


<!-- PHP code -->
< ?php
echo('<p>(PHP) Time: '.date('G:i:s').'');
?>

<!-- JavaScript code -->
<script type="text/javascript">
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
document.write('<p>(JavaScript) Time: '+hours+':'+minutes+':'+seconds+'</p>');
</script>

The above code is part of this page so you can test it against our server, which is set to Central Time USA (same as Chicago).

(PHP) Time: 22:37:45

(JavaScript) Time: 11:42:9

If the two times are nearly or exactly the same, it means the time on the server and the time on your computer are of the same time zone. In that case, you may temporarily adjust the time zone on your computer to see the demonstration with two different time zones.

The demonstration shows that the PHP code runs on the server, not in the browser. It is thus with all PHP code.

If you’ll use your browser to view the source code of a web page with a PHP script, the PHP code is not there. That further demonstrates the PHP code was executed on the server, before the browser ever received the web page source code.

The final form of web page content created or modified by PHP is available to search engine spiders just like static content is.

Here are two tables for reference. One shows where scripts are located. The other where the software runs.

Location of Script CGI PHP JavaScript
Separate from web pages checkmark checkmark
Within web page source code checkmark checkmark
Where script runs CGI PHP JavaScript
On the server checkmark checkmark
In the browser checkmark

Notice that PHP scripts can be within web page source code files and also in files separate from web pages. Either way, the script runs on the server, before any web page source code is sent to the browser.

CGI, JavaScript, and PHP all can create and change web page source code. CGI and PHP would do it at the server before the page is sent to the browser. JavaScript would do it in the browser after the source code arrives.

In general, I would recommend creating/modifying the web page before it reaches the browser using either CGI or PHP. Inserting external files such as a navigation area or content for the specific day are examples. Which one uses depends on what one is most comfortable with. Perl CGI can do pretty much everything PHP can, and some things PHP can’t (or at least I don’t know how to make PHP do them).

Other things must be done with an on page script such as JavaScript. Constantly incremented time display, layers revealed or hidden depending on user choices, and AJAX content retrieval are some examples. In general, if the page needs to change in response to user action and loading a new page is not desired, JavaScript is the tool to do it with.

Will Bontrager

source: http://www.willmaster.com/library/tutorials/functional-differences-between-cgi-javascript-and-php.php

StumbleIt!
Comments

No comments yet.

Leave a comment

(required)

(required)


Spam protection by WP Captcha-Free