CIS 388 Internet Programming

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Introduction to PHP Dr. Charles Severance
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
Introduction to CS520/CS596_026 Lecture Two Gordon Tian Fall 2015.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
PHP A very brief introduction PHP1. PHP = PHP: Hypertext Processor A recursive acronym! PHP scripts are executed on the server side Executed by (a plugin.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative.
Mini – Workshop on PHP Faculty of Engineering in Foreign Languages 1.
Web Systems & Technologies
PHP Basics and Syntax Lesson 3 ITBS2203 E-Commerce for IT.
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
PHP Built-In Functions
CGS 3066: Web Programming and Design Spring 2017
CHAPTER 5 SERVER SIDE SCRIPTING
Introduction to Dynamic Web Programming
CS 371 Web Application Programming
IS1500: Introduction to Web Development
PHP: Login FdSc Module 109 Server side scripting and Database design
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Database Programming Using PHP
DBW - PHP DBW2017.
Web Technologies PHP 5 Basic Language.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Introduction to Web programming
Introduction to Web programming
PHP (PHP: Hypertext Preprocessor)
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
BASIC PHP and MYSQL Edward S. Flores.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
JavaScript.
PHP Overview PHP: Hypertext Preprocessor Server-Side Scripting
Ch. 3. PHP (이 강의 내용의 대부분 예들은 w3schools. com/php/default
ISC440: Web Programming 2 Server-side Scripting PHP 3
Introduction to Web programming
PHP: Security issues FdSc Module 109 Server side scripting and
<?php require("header.htm"); ?>
MySQL Web Application Connecting to a MySQL database
CIS 388 Internet Programming
Software Engineering for Internet Applications
PHP.
Web DB Programming: PHP
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Intro to PHP.
Tutorial 6 PHP & MySQL Li Xu
Tutorial 10: Programming with javascript
MySQL Web Application Connecting to a MySQL database
PHP an introduction.
PHP Language Basics.
PHP-II.
Introduction to Web programming
Conection
PHP Programming Using Cloud 9 IDE.
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

CIS 388 Internet Programming PHP/MySQL Review

What is PHP? PHP is a server-side scripting language designed primarily for web development but also used as a general-purpose programming language. PHP stands for the recursive acronym PHP: Hypertext Preprocessor.[6] PHP code may be embedded into HTML or HTML5 code, or it can be used in combination with various web template systems, web content management systems and web frameworks. PHP code is usually processed by a PHP interpreter implemented as a module in the web server or as a Common Gateway Interface (CGI) executable. The web server combines the results of the interpreted and executed PHP code, which may be any type of data, including images, with the generated web page. PHP code may also be executed with a command-line interface (CLI) and can be used to implement standalone graphical applications.[7] The standard PHP interpreter, powered by the Zend Engine, is free software released under the PHP License. PHP has been widely ported and can be deployed on most web servers on almost every operating system and platform, free of charge.[8] The PHP language evolved without a written formal specification or standard until 2014, leaving the canonical PHP interpreter as a de facto standard. Since 2014 work has gone on to create a formal PHP specification.[9] https://en.wikipedia.org/wiki/PHP

PHP Basics As a scripting language (like javascript), it does not need to be compiled and it can be placed in the page with HTML code between the PHP scripting tags - <?php code ?> or in a separate/included .php file - <?php include(“path/filename.ext”) ?> The syntax is harder to read and more difficult (similar to javascript) The requestor NEVER sees the code, the code is “parsed” (executed) by the server before sending the file to the requestor (secure!) Lines of code are ended with a semi-colon Functions are “contained” within brackets {} (*single-line if-then-else statements do not have to be in brackets, but should for readability) Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration. Functions are not case sensitive, Variables are case sensitive. (*keep it lowercase when in doubt) Files must be saved with a “.php” extension (*include files called from another file can be other file types html/css/rss/etc…, but only “.php” include files will be executed from an external call)

PHP Basics PHP variables should always be declared A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores 

PHP Basics PHP Data/variable types (*not required, loosely typed) String. Integer. Float (floating point numbers - also called double) Boolean. Array. Object. NULL. Resource PHP Operators (+-*/) reference: https://www.w3schools.com/php/php_operators.asp Comments // This is a one-line comment /* This is a multi line comment      yet another line of comment */

PHP Basics Programming Syntax/Structure (Similar to Javascript) Conditionals If-then-else Switch-case Iteration/Loops While/Do…While Loops For/Foreach* Loop (*arrays) Arrays $cars = array("Volvo", "BMW", "Toyota"); Also “0 indexed,” like Javascript reference: https://www.w3schools.com/php/php_arrays.asp

Hello World! PHP “Hello World” <!DOCTYPE HTML> <html>     <head>         <title>Example</title>     </head>      <body>         <?php             echo “Hello World!!";         ?>     </body> </html>

Advanced PHP PHP includes PHP File Handling Including an external php or html file in your PHP page – useful for reusing code/functions in many pages (navigational columns, database connections, session/cookie retrieval and validation etc…) Syntax: include 'filename'; <?php include 'footer.php';?> https://www.w3schools.com/php/php_includes.asp PHP File Handling Open/Read, Create/Modify, and Upload files with PHP https://www.w3schools.com/php/php_file_open.asp fopen(" filename.ext ", “argument") (fread(), fwrite(), and the fclose() are used with fopen) echo readfile(“filename.ext"); (useful for including external data/rss in your page) Uploading files with PHP: https://www.w3schools.com/php/php_file_upload.asp

Advanced PHP PHP Cookies PHP Session Variables PHP cookies are just like cookies in javascript and are used to trore bits of information on the client computer Syntax: setcookie(name, value, expire, path, domain, secure, httponly); "Value is: " . $_COOKIE[$cookie_name]; https://www.w3schools.com/php/php_cookies.asp PHP Session Variables PHP session variables are similar to cookies, they store bits of information on the server, and they are open with each browser “session” that connects to the server. A session is started with the session_start() function. Syntax: $_SESSION[“variablename"] = “value"; "Favorite color is " . $_SESSION["favcolor"] . ".<br>"; https://www.w3schools.com/php/php_sessions.asp

PHP and MySQL PHP allows the web page to get information from and update information stored in a MySQL database Database Connection Code (Object Oriented) <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) {     die("Connection failed: " . $conn->connect_error); }  echo "Connected successfully"; ?> https://www.w3schools.com/php/php_mysql_connect.asp

PHP and MySQL Create Database (*after MySQL Connection) $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) {     echo "Database created successfully"; } else {     echo "Error creating database: " . $conn->error; } https://www.w3schools.com/php/php_mysql_create.asp Create Table (* after Database Created) $sql = "CREATE TABLE MyTable (ColumnName1 TYPE(),…) if ($conn->query($sql) === TRUE) {     echo "Table MyGuests created successfully"; } else {     echo "Error creating table: " . $conn->error; } $conn->close(); https://www.w3schools.com/php/php_mysql_create_table.asp

PHP and MySQL Insert Data into Database $sql = "INSERT INTO MyTable (ColumnName1) VALUES (‘samplevalue')"; if ($conn->query($sql) === TRUE) {     echo "New record created successfully"; } else {     echo "Error: " . $sql . "<br>" . $conn->error; } Select/Display Data from a Database $sql = "SELECT id, ColumnName1 FROM MyTable"; $result = $conn->query($sql); if ($result->num_rows > 0) {     // output data of each row     while($row = $result->fetch_assoc()) {         echo "id: " . $row["id"]. " – Column Value: " . $row[“ColumnName1"]. " "<br>";     } } else {     echo "0 results"; }

PHP and MySQL Delete Records from a Database $sql = "DELETE FROM MyTable WHERE id=1"; if ($conn->query($sql) === TRUE) {     echo "Record deleted successfully"; } else {     echo "Error deleting record: " . $conn->error; } Update Records in a Database $sql = "UPDATE MyTable SET ColumnName1=‘NewValue' WHERE id=1"; if ($conn->query($sql) === TRUE) {     echo "Record updated successfully"; } else {     echo "Error updating record: " . $conn->error; } SQL Reference: https://www.w3schools.com/sql/sql_quickref.asp

PHP Login Form <form action="" method="post" name="Login_Form"> <table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table"> <?php if(isset($msg)){?> <tr> <td colspan="2" align="center" valign="top"><?php echo $msg;?></td> </tr> <?php } ?> <td colspan="2" align="left" valign="top"><h3>Login</h3></td> <td align="right" valign="top">Username</td> <td><input name="Username" type="text" class="Input"></td> <td align="right">Password</td> <td><input name="Password" type="password" class="Input"></td> <td> </td> <td><input name="Submit" type="submit" value="Login" class="Button3"></td> </table> </form>

PHP Login Authentification Code <?php session_start(); /* Starts the session */ /* Check Login form submitted */ if(isset($_POST['Submit'])){ /* Define username and associated password array */ $logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2'); /* Check and assign submitted Username and Password to new variable */ $Username = isset($_POST['Username']) ? $_POST['Username'] : ''; $Password = isset($_POST['Password']) ? $_POST['Password'] : ''; /* Check Username and Password existence in defined array */ if (isset($logins[$Username]) && $logins[$Username] == $Password){ /* Success: Set session variables and redirect to Protected page */ $_SESSION['UserData']['Username']=$logins[$Username]; header("location:index.php"); exit; } else { /*Unsuccessful attempt: Set error message */ $msg="<span style='color:red'>Invalid Login Details</span>"; } ?>

PHP Session Variable Check <?php session_start(); /* Starts the session */ if(!isset($_SESSION['UserData']['Username'])){ header("location:login.php"); exit; } ?> Congratulation! You have logged into password protected page. <a href="logout.php">Click here</a> to Logout.

PHP Logout Session <?php session_start(); /* Starts the session */ session_destroy(); /* Destroy started session */ header("location:login.php"); /* Redirect to login page */ exit; ?>

PHP Reserved Words List of Keywords These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on--but they're not , really: they are language constructs. You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion. Table K-1. PHP Keywords and or xor __FILE__ exception (PHP 5) __LINE__ array() as break case class const continue declare default die() do echo() else elseif empty() enddeclare endfor endforeach endif endswitch endwhile eval() exit() extends for foreach function global if include() include_once() isset() list() new print() require() require_once() return() static switch unset() use var while __FUNCTION__ __CLASS__ __METHOD__ final (PHP 5) php_user_filter(PHP 5) interface (PHP 5) implements (PHP 5) public (PHP 5) private (PHP 5) protected (PHP 5) abstract (PHP 5) clone (PHP 5) try (PHP 5) catch (PHP 5) throw (PHP 5) cfunction (PHP 4 only) old_function (PHP 4 only) this (PHP 5 only)   Source: http://www.nusphere.com/kb/phpmanual/reserved.htm

Additional Resources W3 Schools PHP tutorial: https://www.w3schools.com/php/ PHP tutorials: http://www.freewebmasterhelp.com/tutorials/php https://www.wired.com/2010/02/php_tutorial_for_beginners/ http://www.homeandlearn.co.uk/php/php.html https://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/ http://www.tutorialspoint.com/php/php_tutorial.pdf http://php.net/manual/en/pdf.examples.php https://www.youtube.com/watch?v=7TF00hJI78Y PHP scripts and code resources: http://www.java2s.com/Code/Php/CatalogPhp.htm http://www.wellho.net/mouth/1722_PHP-examples-source-code-and-try-it-out-too.html https://www.freshdesignweb.com/php-tutorial-and-examples-for-beginners/ https://downloads.mysql.com/docs/apis-php-en.pdf PHP IDE resources: http://stackoverflow.com/questions/116292/what-is-the-best-ide-for-php http://www.w3schools.in/php-script/php-login-without-using-database/ http://www.coderslexicon.com/really-simple-php-login-logout-script-example/