Week 05b - PHP
Week 05b - PHP
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8"/>
<title>PHP Test</title>
</head>
<body>
<p>Hello World</p></body>
</html>
CS 312 - Internet Concepts / Weigle 7
Simple PHP Form Processing Example
• HTML document calling a PHP script
<html><body>
<form action="action.php" method="post">
<p>Hi John!</p>
<p>You are 21 years old.</p>
~/secure_html/cs312/examples/php/action.php
<p>Hi <?php echo htmlspecialchars($_POST['name']); ?>!
</p>
<p>You are <?php echo (int)$_POST['age']; ?> years
old.</p>
action2.php
<?php
echo "<p> Hi ";
echo These PHP scripts are
htmlspecialchars($_POST['name']); functionally equivalent.
echo "!</p>";
echo "<p>You are ";
echo (int)$_POST['age'];
echo " years old.</p>";
?> https://www.cs.odu.edu/~mweigle/cs312/examples/php/php-form2.html
CS 312 - Internet Concepts / Weigle 12
Process Summary
• PHP scripts are executed on the web server
Using POST 21
action.php
<p>Hi <?php echo
htmlspecialchars
($_POST['name']); ?
>!</p>
<p>You are <?php echo
(int)
$_POST['age']; ?>
years old.</p>
Using GET 21
action-get.php
<p>Hi <?php echo
htmlspecialchars
($_GET['name']); ?
>!</p>
<p>You are <?php echo
(int) $_GET['age'];
?> years old.</p>
• strpos(String, SearchString)
– returns the starting position of SearchString if found inside
String
• position starts from 0 (i.e., 0 is the first character)
– returns FALSE if SearchString is not found in String
http://www.w3schools.com/PHP/php_string.asp
CS 312 - Internet Concepts / Weigle 20
PHP Numeric Arrays
• Creation
$names = array("Peter", "Quagmire", "Joe");
OR
$names[0] = "Peter"; Length of an array:
$names[1] = "Quagmire"; count (array)
$names[2] = "Joe";
• Usage count ($names)
3
$names[1] Quagmire
http://www.w3schools.com/PHP/php_arrays.asp
CS 312 - Internet Concepts / Weigle 21
PHP Associative Arrays
• Creation
$ages = array("Peter"=>"32", "Quagmire"=>"30",
"Joe"=>"34");
OR
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
• Usage
$ages['Peter'] 32
http://www.w3schools.com/PHP/php_arrays.asp
CS 312 - Internet Concepts / Weigle 22
Outline
• PHP and HTML Interaction
• PHP Basic Syntax
• PHP Conditionals, Loops, Functions
• User Registration Example
Ref: https://www.w3schools.com/php/php_ref_regex.asp
CS 312 - Internet Concepts / Weigle 39
PHP Form Handler
1. Present the HTML form to the user register.html
text file
• set up and return
response
CS 312 - Internet Concepts / Weigle 43
PHP Form Handler
1. Present the HTML form to the user register.html