WEB DEVELOPMENT And Applications PHP (Hypertext Preprocessor) By: Gheyath M. Othman
PHP Forms PHP Form Handling: The PHP superglobals $_GET and $_POST are used to collect form-data. • The example below displays a simple HTML form with two input fields and a submit button: • When the user fills out the form and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. <!DOCTYPE HTML><html><body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit“ name=‘submit’ value=‘submit’> </form> </body></html> <?php echo “Welocme”.$_POST["name"]; echo “<br>Your email address is: “ .$_POST["email"]; ?> HTML PHP file
PHP Forms GET vs. POST • Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user. • Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. • $_GET is an array of variables passed to the current script via the URL parameters. • $_POST is an array of variables passed to the current script via the HTTP POST method.
PHP Forms When to use GET? Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. GET may be used for sending non-sensitive data. Note: GET should NEVER be used for sending passwords or other sensitive information!
PHP Forms When to use POST? Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send. Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server. However, because the variables are not displayed in the URL, it is not possible to bookmark the page. Notes: Developers prefer POST for sending form data.
PHP Forms Example: <!DOCTYPE HTML> <html> <body> <h2>PHP Form Sumbit Example</h2> <form method="post" action="<?php $_SERVER[‘PHP_SELF’];?>"> Name: <input type="text" name="name"> <br><br> E-mail: <input type="text" name="email"><br> Address: <input type="text" name="address"><br> Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> HTML After submitting form it will return to the same page, also you can use the page name itself or leave it empty if you want to send information to the same page.
PHP Forms Example: continued <?php $name = $email = $gender = $comment = $address = ""; if (isset($_POST['submit'])) { $name = $_POST["name"]; $email = $_POST["email"]; $address = $_POST["address"]; $comment = $_POST["comment"]; $gender = $_POST["gender"]; echo "<h2>Your Input:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $address; echo "<br>"; echo $comment; echo "<br>"; echo $gender; } ?> PHP Initiate variables When the submit button in the form is clicked The method is POST so use $_POST to get text box values Output the form values to the browser..
PHP Forms What is the $_SERVER["PHP_SELF"] variable? • The $_SERVER["PHP_SELF"] is a super global variable that returns the filename of the currently executing script. • So, the $_SERVER["PHP_SELF"] sends the submitted form data to the page itself, instead of jumping to a different page. This way, the user will get error messages on the same page as the form. What is the htmlspecialchars() function? The htmlspecialchars() function converts special characters to HTML entities. This means that it will replace HTML characters like < and > with &lt; and &gt;. This prevents attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attacks) in forms. <form method="post" action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>">
PHP Forms (validation) Example: required fields: <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <h2>PHP Form Validation Example</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name"><span class="error">*<?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email"><span class="error">*<?php echo $emailErr;?></span> <br><br> Address: <input type="text" name="address"><span class="error">*<?php echo $addressErr;?></span> <br><br> Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> HTML FORM Note for more information about validation(name, email, websites) visit www.w3schools.com/php/php_form_url_email.html
PHP Forms(validation) Example: required fields: Validation with PHP <?php $nameErr = $emailErr = $genderErr = $addressErr = ""; $name = $email = $gender = $comment = $address = ""; if (isset($_POST['submit'])) { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = $_POST["name"]; } if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = $_POST["email"]; } if (empty($_POST["address"])) { $addressErr = "address is required"; } else { $address = $_POST["address"]; } if (empty($_POST["comment"])) { $comment = ""; } else { $comment =$_POST["comment"]; } if (empty($_POST["gender"])) { $genderErr = "Gender is required"; } else { $gender = $_POST["gender"]; } echo "<h2>Your Input:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $address; echo "<br>"; echo $comment; echo "<br>"; echo $gender; } ?>
PHP Date and Time • The PHP date() function is used to format a date and/or a time. • The PHP date() function formats a timestamp to a more readable date and time. Syntax date(format,timestamp) Parameter Description format Required. Specifies the format of the timestamp timestamp Optional. Specifies a timestamp. Default is the current date and time NOTE: A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
PHP Date and Time Get a Simple Date The required format parameter of the date() function specifies how to format the date (or time). Here are some characters that are commonly used for dates: • d - Represents the day of the month (01 to 31) • m - Represents a month (01 to 12) • Y - Represents a year (in four digits) • l (lowercase 'L') - Represents the day of the week Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.
PHP Date and Time <!DOCTYPE html> <html> <body> <?php echo "Today is " . date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") . "<br>"; echo "Today is " . date("Y-m-d") . "<br>"; echo "Today is " . date("l"); ?> </body> </html> The example below formats today's date in three different ways: <!DOCTYPE html> <html> <body> &copy; 2010-<?php echo date("Y");?> </body> </html> Automatic copy date:
PHP Date and Time Get a Simple Time Here are some characters that are commonly used for times: • h - 12-hour format of an hour with leading zeros (01 to 12) • H- 24-hours format • i - Minutes with leading zeros (00 to 59) • s - Seconds with leading zeros (00 to 59) • a - Lowercase Ante meridiem and Post meridiem (am or pm) • A- Uppercase Ante meridiem and Post meridiem (AM or PM) Note that the PHP date() function will return the current date/time of the server!
PHP Date and Time The example below outputs the current time in the specified format: <!DOCTYPE html> <html> <body> <?php echo "The time is " . date("h:i:s a"); ?> </body> </html>
PHP Date and Time There are other functions like: mktime and strtotime: <!DOCTYPE html><html><body> <?php $d=mktime(11, 14, 54, 18, 02, 2017); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?> </body></html> Note for more information on date/time visit ww.w3schools.com/php/php_date.html mktime(hour,minute,second,month,day,year) Created date is 2017-02-18 11:14:54am strtotime(time,now) <!DOCTYPE html><html><body> <?php $d=strtotime("10:30pm April 18 2017"); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?> </body></html> Created date is 2017-04-18 10:30:00pm
PHP Include & Required Files • The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. • Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website. • It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: • require will produce a fatal error (E_COMPILE_ERROR) and stop the script • include will only produce a warning (E_WARNING) and the script will continue Syntax: include 'filename'; or require 'filename';
PHP Include & Required Files Example-1- : Assume we have a standard footer file called "footer.php", that looks like this: the filename is footer.php <?php echo "<p>Copyright &copy; 2010-" . date("Y") . " AkreIT.com</p>"; ?> <!DOCTYPE html> <html> <body> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> <?php include 'footer.php';?> </body> </html>
PHP Include & Required Files Example-2- Assume we have a standard menu file called "menu.php": <?php echo '<a href="home.php">Home</a> - <a href=" home.php ">HTML Tutorial</a> - <a href=" home.php ">CSS Tutorial</a> - <a href=" home.php ">JavaScript Tutorial</a> - <a href=" home.php ">PHP Tutorial</a>'; ?> <!DOCTYPE html> <html> <body> <div class="menu"> <?php require 'menu.php';?> </div> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> </body> </html>
PHP Include & Required Files PHP include vs. require there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute. When the require statement is used, if the PHP doesn’t find the file it will stop the execution and dies after the require statement returned a fatal error NOTE: Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found.

Web Development Course: PHP lecture 2

  • 1.
    WEB DEVELOPMENT And Applications PHP(Hypertext Preprocessor) By: Gheyath M. Othman
  • 2.
    PHP Forms PHP FormHandling: The PHP superglobals $_GET and $_POST are used to collect form-data. • The example below displays a simple HTML form with two input fields and a submit button: • When the user fills out the form and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. <!DOCTYPE HTML><html><body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit“ name=‘submit’ value=‘submit’> </form> </body></html> <?php echo “Welocme”.$_POST["name"]; echo “<br>Your email address is: “ .$_POST["email"]; ?> HTML PHP file
  • 3.
    PHP Forms GET vs.POST • Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user. • Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. • $_GET is an array of variables passed to the current script via the URL parameters. • $_POST is an array of variables passed to the current script via the HTTP POST method.
  • 4.
    PHP Forms When touse GET? Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. GET may be used for sending non-sensitive data. Note: GET should NEVER be used for sending passwords or other sensitive information!
  • 5.
    PHP Forms When touse POST? Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send. Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server. However, because the variables are not displayed in the URL, it is not possible to bookmark the page. Notes: Developers prefer POST for sending form data.
  • 6.
    PHP Forms Example: <!DOCTYPE HTML><html> <body> <h2>PHP Form Sumbit Example</h2> <form method="post" action="<?php $_SERVER[‘PHP_SELF’];?>"> Name: <input type="text" name="name"> <br><br> E-mail: <input type="text" name="email"><br> Address: <input type="text" name="address"><br> Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> HTML After submitting form it will return to the same page, also you can use the page name itself or leave it empty if you want to send information to the same page.
  • 7.
    PHP Forms Example: continued <?php $name= $email = $gender = $comment = $address = ""; if (isset($_POST['submit'])) { $name = $_POST["name"]; $email = $_POST["email"]; $address = $_POST["address"]; $comment = $_POST["comment"]; $gender = $_POST["gender"]; echo "<h2>Your Input:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $address; echo "<br>"; echo $comment; echo "<br>"; echo $gender; } ?> PHP Initiate variables When the submit button in the form is clicked The method is POST so use $_POST to get text box values Output the form values to the browser..
  • 8.
    PHP Forms What isthe $_SERVER["PHP_SELF"] variable? • The $_SERVER["PHP_SELF"] is a super global variable that returns the filename of the currently executing script. • So, the $_SERVER["PHP_SELF"] sends the submitted form data to the page itself, instead of jumping to a different page. This way, the user will get error messages on the same page as the form. What is the htmlspecialchars() function? The htmlspecialchars() function converts special characters to HTML entities. This means that it will replace HTML characters like < and > with &lt; and &gt;. This prevents attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attacks) in forms. <form method="post" action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>">
  • 9.
    PHP Forms (validation) Example:required fields: <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <h2>PHP Form Validation Example</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name"><span class="error">*<?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email"><span class="error">*<?php echo $emailErr;?></span> <br><br> Address: <input type="text" name="address"><span class="error">*<?php echo $addressErr;?></span> <br><br> Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> HTML FORM Note for more information about validation(name, email, websites) visit www.w3schools.com/php/php_form_url_email.html
  • 10.
    PHP Forms(validation) Example: requiredfields: Validation with PHP <?php $nameErr = $emailErr = $genderErr = $addressErr = ""; $name = $email = $gender = $comment = $address = ""; if (isset($_POST['submit'])) { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = $_POST["name"]; } if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = $_POST["email"]; } if (empty($_POST["address"])) { $addressErr = "address is required"; } else { $address = $_POST["address"]; } if (empty($_POST["comment"])) { $comment = ""; } else { $comment =$_POST["comment"]; } if (empty($_POST["gender"])) { $genderErr = "Gender is required"; } else { $gender = $_POST["gender"]; } echo "<h2>Your Input:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $address; echo "<br>"; echo $comment; echo "<br>"; echo $gender; } ?>
  • 11.
    PHP Date andTime • The PHP date() function is used to format a date and/or a time. • The PHP date() function formats a timestamp to a more readable date and time. Syntax date(format,timestamp) Parameter Description format Required. Specifies the format of the timestamp timestamp Optional. Specifies a timestamp. Default is the current date and time NOTE: A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
  • 12.
    PHP Date andTime Get a Simple Date The required format parameter of the date() function specifies how to format the date (or time). Here are some characters that are commonly used for dates: • d - Represents the day of the month (01 to 31) • m - Represents a month (01 to 12) • Y - Represents a year (in four digits) • l (lowercase 'L') - Represents the day of the week Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.
  • 13.
    PHP Date andTime <!DOCTYPE html> <html> <body> <?php echo "Today is " . date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") . "<br>"; echo "Today is " . date("Y-m-d") . "<br>"; echo "Today is " . date("l"); ?> </body> </html> The example below formats today's date in three different ways: <!DOCTYPE html> <html> <body> &copy; 2010-<?php echo date("Y");?> </body> </html> Automatic copy date:
  • 14.
    PHP Date andTime Get a Simple Time Here are some characters that are commonly used for times: • h - 12-hour format of an hour with leading zeros (01 to 12) • H- 24-hours format • i - Minutes with leading zeros (00 to 59) • s - Seconds with leading zeros (00 to 59) • a - Lowercase Ante meridiem and Post meridiem (am or pm) • A- Uppercase Ante meridiem and Post meridiem (AM or PM) Note that the PHP date() function will return the current date/time of the server!
  • 15.
    PHP Date andTime The example below outputs the current time in the specified format: <!DOCTYPE html> <html> <body> <?php echo "The time is " . date("h:i:s a"); ?> </body> </html>
  • 16.
    PHP Date andTime There are other functions like: mktime and strtotime: <!DOCTYPE html><html><body> <?php $d=mktime(11, 14, 54, 18, 02, 2017); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?> </body></html> Note for more information on date/time visit ww.w3schools.com/php/php_date.html mktime(hour,minute,second,month,day,year) Created date is 2017-02-18 11:14:54am strtotime(time,now) <!DOCTYPE html><html><body> <?php $d=strtotime("10:30pm April 18 2017"); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?> </body></html> Created date is 2017-04-18 10:30:00pm
  • 17.
    PHP Include &Required Files • The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. • Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website. • It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: • require will produce a fatal error (E_COMPILE_ERROR) and stop the script • include will only produce a warning (E_WARNING) and the script will continue Syntax: include 'filename'; or require 'filename';
  • 18.
    PHP Include &Required Files Example-1- : Assume we have a standard footer file called "footer.php", that looks like this: the filename is footer.php <?php echo "<p>Copyright &copy; 2010-" . date("Y") . " AkreIT.com</p>"; ?> <!DOCTYPE html> <html> <body> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> <?php include 'footer.php';?> </body> </html>
  • 19.
    PHP Include &Required Files Example-2- Assume we have a standard menu file called "menu.php": <?php echo '<a href="home.php">Home</a> - <a href=" home.php ">HTML Tutorial</a> - <a href=" home.php ">CSS Tutorial</a> - <a href=" home.php ">JavaScript Tutorial</a> - <a href=" home.php ">PHP Tutorial</a>'; ?> <!DOCTYPE html> <html> <body> <div class="menu"> <?php require 'menu.php';?> </div> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> </body> </html>
  • 20.
    PHP Include &Required Files PHP include vs. require there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute. When the require statement is used, if the PHP doesn’t find the file it will stop the execution and dies after the require statement returned a fatal error NOTE: Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found.