Promo Offers Black Friday Offers:
OpenVS VPS up to 60% OFF for 12 months Learn More Semi-Dedicated up to 86% OFF for 3 months Learn More
  • Your Cart is Empty
  • Cart
  • Log In

PHP Script

PHP was established as the leading website programming language several years ago, even though it is much younger than other languages. The cause of the extensive popularity the PHP distribution enjoys is the easy-to-grasp syntax, allowing even people with no coding experience whatsoever to quickly enter into the PHP realm. And it is exactly the easily created scripts that make PHP so popular among the Internet community.

PHP Scripts

A sample PHP script:

<?php
echo '<p>This is a PHP script</p>';
?>

PHP scripts can be created using any basic text editor or HTML editing software tool. Each PHP file must be saved with a .php file extension in order to be recognized as a functioning PHP script. When the Apache server has the appropriate settings, PHP code can be recognized also in .html files. This can also be achieved by adding an additional handler in the .htaccess file of a Linux based web server.

A PHP script for deleting files via an FTP connection:

<?php
$file = 'file_to_delete.txt';

// set up basic ftp connection
$conn_id = ftp_connect($my_ftp_server);

// login with my username and password
ftp_login($conn_id, $my_ftp_user, $my_ftp_user);

// try to delete $file
if (ftp_delete($conn_id, $file)) {
echo "$file deleted successful\n";
} else {
echo "could not delete $file\n";
}

// close the ftp connection
ftp_close($conn_id);

?>

A PHP script can be treated as an HTML page with bits of PHP inserted here and there. Anything part of a PHP script that is not contained within a <?php ?> tag is ignored by the PHP compiler and passed directly to the web browser.

PHP in an HTML file

<html>
<head></head>
<body class="page_bg">
Hello, today is <?php echo date('l, F jS, Y'); ?>.
</body>
</html>

PHP scripts and database interaction

The PHP language has been especially designed to enable the development of dynamic and interactive web pages. PHP offers excellent connectivity with many databases, among them MySQL, PostgreSQL and Generic ODBC. The common combination between the PHP v.4 or the PHP v.5 script and the MySQL database is supported on almost every Linux-based web server.

Connecting to a MySQL database with a PHP script:

<?php
$dbhandle = mysql_connect('localhost', 'phptest', '3579php');
if ($dbhandle)
{
echo "Connected to MySQL Database<br>";
mysql_close($dbhandle);
} else {
echo "Unable to connect to MySQL Database<br>";
}
?>

PHP scripts and email

As a web-oriented scripting language, PHP contains all the necessary functionalities to perform different Internet operations. It is exceptionally easy indeed to create a PHP script for connecting to remote servers, for checking email via POP3 or IMAP, for URL encoding, setting cookies, redirecting, etc. PHP is extensively used for the creation and operation of online forms and automated email services.

Here you can see the combination of a pure HTML input form and a corresponding PHP script, which will handle the form’s data.

A sample HTML form:

<html>
<head><title>Example Form</title></head>
<body>
<form action="mail.php" method="POST">
<b>Email address</b><br>
<input type="text" name="email" size=40>
<p><b>Subject</b><br>
<input type="text" name="subject" size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name="message"></textarea>
<p><input type="submit" value=" Send your email ">
</form>
</body>
</html>

And here is the PHP code, which will handle and process the information:

A PHP code that handles the form's input:

<?php

/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $POST_['email'];
$subject = $POST_['subject'];
$message = $POST_['message'];

/* PHP form validation: the following lines will check if the information in the input fields is valid. This check uses regular expressions to verify the input data. Have in mind that PHP form validation is performed AFTER the form details have been submitted. It's best if it's used with JavaScript form validation, which is executed in the visitor's browser. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
  echo "<h4>Thank you for sending email</h4>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>

PHP scripts and NTC Hosting

All abovementioned features have made PHP one of the most widely preferred web development languages. There are many extremely popular PHP script-based web page, and also a large number of PHP-based web applications. Being an open-source product, PHP lies at the bases of many popular free web applications, such as Joomla, WordPress, the phpBB forum, OsCommerce, and many others.

In order to protect the developer's code and also - to provide higher security for business and enterprise applications, all PHP scripts run on NTC Hosting's web hosting servers can be encrypted with the Zend Guard and ionCube PHP Encoder tools.