Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP loeng 2.

Similar presentations


Presentation on theme: "PHP loeng 2."— Presentation transcript:

1 PHP loeng 2

2 <html> <head><title>Random</title></head> <body> <p>I have randomly selected the number <?php $choice = rand(1, 100); echo $choice; ?>. Its square root is <?php echo sqrt($choice); ?>.</p> </body> </html>

3 Accessing user information. Creating forms
<form method="post" action="random.php">  <p>Range Start: <input type="text" name="begin" /></p>  <p>Range End: <input type="text" name="end" /></p>  <p><input type="submit" value="Generate" /></p>  </form> 

4 Accessing user information. Continue
<?php import_request_variables("pg", "form_"); ?> <html> <head> <title>Generate Random Number</title> </head> <body> <p>From the range <?php echo $form_begin; ?> to <?php echo $form_end; ?> I have selected the random number <?php echo rand($form_begin, $form_end); ?>.</p> </body> </html>

5 Input validation. The preg_match function
if(preg_match("/^[0-9]{5}$/", $form_zipcode)) {      echo "The ZIP code must be a 5-digit number.";  }  sequence of digits will return 1 if that sequence appears anywhere in the string string starting and ending with a slash ('/') ^ start of string

6 Regular Expressions. Continue with validation
if(preg_match("^[A-Z]{2}$", $lname)) {      echo "The ZIP code must be a 5-digit number.";  }

7 Regular Expressions () grouping [] range of characters . any character
{} copies of the preceding pattern ? zero or one of the preceding pattern * any number of the preceding pattern (including zero) + at least one of the preceding pattern ^ start of string $ end of string \ treat next character literally instead of as a special symbol

8 Simple contact form <html><body> <form action="myform.php" method="post"> <p>Your Name: <input type="text" name="yourname" /><br /> <input type="text" name=" " /></p> <p>Do you like this website? <input type="radio" name="likeit" value="Yes" checked="checked" /> Yes <input type="radio" name="likeit" value="No" /> No <input type="radio" name="likeit" value="Not sure" /> Not sure</p> <p>Your comments:<br /> <textarea name="comments" rows="10" cols="40"></textarea></p> <p><input type="submit" value="Send it!"></p> </form> </body></html>

9 All variables passed to the current script via the HTTP POST method are stored in associative array $_POST.

10 Simple contact form. Script
<html> <body> Your name is: <?php echo $_POST['yourname']; ?><br /> Your <?php echo $_POST[' ']; ?><br /> <br /> Do you like this website? <?php echo $_POST['likeit']; ?><br /> Comments:<br /> <?php echo $_POST['comments']; ?> </body> </html>

11 Validating forms with PHP. htmlspecialchars()
<?php $yourname = htmlspecialchars($_POST['yourname']); $ = htmlspecialchars($_POST[' ']); $likeit = htmlspecialchars($_POST['likeit']); $comments = htmlspecialchars($_POST['comments']); ?> <html><body> Your name is: <?php echo $yourname; ?><br /> Your <?php echo $ ; ?><br /><br /> Do you like this website? <?php echo $likeit; ?><br /><br /> Comments:<br /> <?php echo $comments; ?> </body></html> This function will replace HTML chars like < and > to their HTML version < and >.

12 Why we need the htmlspecialchars() ? Example.
<script>location.href('

13 What else to check? Let's do two more things: 1. strip unnecessary characters from the data if quotes are escaped with a slash \ let's remove that.

14 $yourname = check_input($_POST['yourname']);
<?php $yourname = check_input($_POST['yourname']); $ = check_input($_POST[' ']); $likeit = check_input($_POST['likeit']); $comments = check_input($_POST['comments']); ?> <html><body> Your name is: <?php echo $yourname; ?><br /> Your <?php echo $ ; ?><br /><br /> Do you like this website? <?php echo $likeit; ?><br /><br /> Comments:<br /> <?php echo $comments; ?> </body></html> function check_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; }?>

15 Let's edit the check_input function from the previous page
function check_input($data, $problem=' ') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) die($problem); } return $data; By default $problem is empty $yourname = check_input($_POST['yourname'], "Enter your name");

16 <?php $yourname = check_input($_POST['yourname'], "Enter your name"); $ = check_input($_POST[' ']); $likeit = check_input($_POST['likeit']); $comments = check_input($_POST['comments'], "Write your comments"); ?> <html><body> Your name is: <?php echo $yourname; ?><br /> Your <?php echo $ ; ?><br /><br /> Do you like this website? <?php echo $likeit; ?><br /><br /> Comments:<br /> <?php echo $comments; ?> </body></html> <?php function check_input($data, $problem=''){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { die($problem); } return $data; ?>

17 <?php function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); }

18 Validate e-mail address
$ = htmlspecialchars($_POST[' ']); if { die(" address not valid"); } Character Description a single character \s a whitespace character (space, tab, newline) \S non-whitespace character \d a digit (0-9) \D a non-digit \w a word character (a-z, A-Z, 0-9, _) \W a non-word character p+ It matches any string containing at least one p. \w\- \w match any word character [a-zA-Z0-9_] \- matches the character - literally

19 FILTER_VALIDATE <!DOCTYPE html> <html><body> <?php // Variable to check $ = // Validate if (!filter_var($ , FILTER_VALIDATE_ ) === false) { echo("$ is a valid address"); } else { echo("$ is not a valid address"); } ?> </body></html>

20 $pattern = "^[_a-z0-9-]+(\. [_a-z0-9-]+). @[a-z0-9-]+(\. [a-z0-9-]+)
$pattern =

21 Validate URL address $url = htmlspecialchars($_POST['website']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$url)) { die("URL address not valid"); }

22 Digits 0-9 only if (preg_match("/\D/",$age)) { die("Please enter numbers only for Age"); }

23 Letters a-z and A-Z only (no spaces, digits or any other characters)
if (preg_match("/[^a-zA-Z]/",$text)) { die("Please enter letters a-z and A-Z only!"); }


Download ppt "PHP loeng 2."

Similar presentations


Ads by Google