Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Hypertext PreProcessor. Documentation Available www.php.netwww.w3schools.com SAMS books O’Reilly Books.

Similar presentations


Presentation on theme: "PHP Hypertext PreProcessor. Documentation Available www.php.netwww.w3schools.com SAMS books O’Reilly Books."— Presentation transcript:

1 PHP Hypertext PreProcessor

2 Documentation Available www.php.netwww.w3schools.com SAMS books O’Reilly Books

3 Why PHP? PHP is a scripting language, a server-side language Used to transmit form data from HTML to database Has ability to place files on client machines

4 PHP Variables All variables in PHP begin with a $ PHP is VERY case-sensitive for all things Types include: Boolean, Integer, Float, String, Array, Object… PHP Delimiters (most common method) (most common method) (lazy method) (lazy method)

5 PHP Super Global Varibles Always there for us to use with declaration $_COOKIE, $_FILES, $_GET, $_POST, $_REQUEST, $_SERVER, $_SESSION PHP pages can contain only HTML All pages including ANY PHP must have a.php extension

6 Our Own Google… Create mygoogle.html page with simple input text field and variable name = q and a submit button The action of mygoogle.html should be mygoogle.php We are going to send the form data (via GET) to the php page Create mygoogle.php to gather and repeat q’s value using a print or print_r with Create mygoogle.php to gather and repeat q’s value using a print or print_r with Change action to http://www.google.com/search so it’ll actually search Google

7 What Else Can PHP Do? Grab data from an HTML form Grab data from a file on web server via readfile(“…”) or file_get_contents(‘…’) A Single Search Page for ALL…… mysearchpage.html (includes JS ‘onclick’) ie: http://192.168.254.124/~instructor/CIS115-Spring2011/Lecture2/mysearch/mysearchJAVA.html mysearchpage.php (uses PHP case statements) ie: http://192.168.254.124/~instructor/CIS115-Spring2011/Lecture2/mysearch/mysearchpagerevised.html Which is better? For what reason?

8 PHP Registration Form Use an HTML document (register.html) with form sending data to PHP document (register.php) for processing and confirmation Use POST method to prevent eavesdropping Use PHP Form Validation (similar in nature to JS validation) Send email to administrator and registrant containing form data, then display “Successful” message or “Unsuccessful” message depending on email status (using if…else conditional)

9 PERL Regular Expression Matching preg_match in PHP for conditional validation if(!preg_match(“/^.*@.*sampsoncc\.edu$/”, $_POST[“email”])) { …do something if it’s not a *@*sampsoncc.edu email address } else { …do something if it is }

10 Cookies, Sessions, SSL Cookie: temporary data stored on the client side which is sent to the server to remind the server what data is associated with a particular user Programmers have control over the type of cookie, the lifetime of the cookie, and the function of the cookie Session Cookies: valid while the browser is open / pseudo-random string of numbers and letters Persistent Cookie: valid for the period of time set by the programmer / can contain any data

11 Session Cookies $_SESSION is a PHP variable that can store whatever information you want To use sessions, we need to call the “session_start()” function at the top of our PHP code. When you call this function, the default cookie name is PHPSESSID containing a pseudo-random string

12 Session Cookie Demo Pages home.php – uses session cookies login1.php – uses session cookies, hard-coded UN/PW, form submits to itself via $_SERVER[“PHP_SELF”] login2.php – similar to login1.php but tells user login is invalid, pre-fills username with last value for convenience

13 Persistent Cookies Way of storing information as a file on the user’s machine for longer terms of time Uses “session_start()” at the top of the PHP code, as well as “setcookie” to set the cookie, the information it contains, and the time it will be active on the client’s machine

14 Persistent Cookie Demo Pages login3.php – looks for a persistent cookie for automatic login, adds persistent cookie for username, pre-fills username from cookie if login required login4.php – sets cookie for username, allows you to choose to include password in cookie, can add spyware using cookie, autologin if UN/PW in cookie are valid, grabs UN/PW entered and adds it to a new cookie logout.php – deletes cookies from machine, logs the user out

15 SSL or HTTPS Logins Login pages should incorporate SSL or HTTPS where login information is sent encrypted Use RewriteEngine in Apache to redirect users RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.mypage\.com [NC] RewriteRule (.*) http://www.mypage.com/$1 [R=301,L] RewriteCond %{REQUEST_URI} ^/login/ RewriteCond %{HTTPS} != on RewriteRule (.*) https://www.mypage.com/$1 [R=301,L]

16 User Survey We want to create a user survey that will ask the user questions, and when submitted, email the responses to ourselves and the user. Then give a confirmation message to the user … all with PHP and HTML!


Download ppt "PHP Hypertext PreProcessor. Documentation Available www.php.netwww.w3schools.com SAMS books O’Reilly Books."

Similar presentations


Ads by Google