PHP-MySQL By Jonathan Foss. PHP and MySQL Server Web Browser Apache PHP file PHP MySQL Client Recall the PHP architecture PHP can communicate with a MySQL.

Slides:



Advertisements
Similar presentations
Introduction The concept of “SQL Injection”
Advertisements

E-Invoice System CS541 Database Systems Class Project By: Parul Gupta.
PHP and MySQL PHP for the Web, page PHP and MySQL MySQL Resource PHP – MySQL Resource
Objectives Connect to MySQL from PHP
Website Development & Management PHP Odds & Ends Instructor: John Seydel, Ph.D. CIT Fall
Sara SartoliAkbar Siami Namin NSF-SFS workshop July 14-18, 2014.
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools.
SQL-Injection attacks Damir Lizdek & Dan Rundlöf Language-based security.
Check That Input Preventing SQL Injection Attacks By Andrew Morton For CS 410.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
PHP Security.
Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side.
What is MySQL? MySQL is a database. The data in MySQL is stored in database objects called tables. A table is a collections of related data entries and.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
MIS Week 11 Site:
LIS651 lecture 7 PHP mySQL Thomas Krichel
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
PHP – MySQL Extensions. Table used in most examples CREATE TABLE product ( rowID INT NOT NULL AUTO_INCREMENT, productid VARCHAR(8) NOT NULL, name VARCHAR(25)
INTERNET APPLICATION DEVELOPMENT PRACTICAL ON CONNECTING TO MYSQL.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
PHP Workshop ‹#› PHP Security. PHP Workshop ‹#› Two Golden Rules 1.FILTER external input Obvious.. $_POST, $_COOKIE, etc. Less obvious.. $_SERVER 2.ESCAPE.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
Prof Frankl, Spring 2008CS Polytechnic University 1 Overview of Web database applications with PHP.
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured.
Intro to LAMP Programming Presented for SAT Linux Users' Group by Dan Zollars.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Retrieving data from MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
PHP Database Processing CIS 1715 Web Technologies.
Security Considerations Steve Perry
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
Group Name: PNT Group Members: Prabin Joshi and Ngoc Vu.
DataFlow Diagram – Level 0
WEB SECURITY WEEK 2 Computer Security Group University of Texas at Dallas.
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CHAPTER 10 PHP MySQL Database
Preventing MySQL Injection Sonja Parson COSC 5010 Security Presentation April 26, 2005.
Secure Authentication. SQL Injection Many web developers are unaware of how SQL queries can be tampered with SQL queries are able to circumvent access.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Example – SQL Injection MySQL & PHP code: // The next instruction prompts the user is to supply an ID $personID = getIDstringFromUser(); $sqlQuery = "SELECT.
PHP MySQL1 PHP and MySQL After this lecture, you should be able to:  Access a MySQL database with PHP  mysql_connect()  mysql_select_db()  mysql_query()
Web Systems & Technologies
Introduction to Dynamic Web Programming
Unix System Administration
SQL INJECTION ATTACKS.
Database Driven Websites
Web Systems Development (CSC-215)
Web Systems Development (CSC-215)
Erasmus Exchange in Ionian University
PHP: Security issues FdSc Module 109 Server side scripting and
Lecture 2 - SQL Injection
Web Programming– UFCFB Lecture
PHP Forms and Databases.
Presentation transcript:

PHP-MySQL By Jonathan Foss

PHP and MySQL Server Web Browser Apache PHP file PHP MySQL Client Recall the PHP architecture PHP can communicate with a MySQL server (either local or external)

PHP and MySQL To start a connection use mysql_connect(): mysql_connect(“localhost”,”root”,”password”) or die(“Error connecting to mysql”); Then select a database using mysql_select_db() mysql_select_db(“mydatabase”); To perform a query use mysql_query(): $query = “SELECT id, FROM users where name=\”Jonny\””; $res = mysql_query($query);

MySQL Results mysql_query() returns results in a result structure $query = “SELECT id, FROM users where name=\”Jonny\””; $res = mysql_query($query); Need to fetch rows from the result before use We can use either mysql_fetch_row() or mysql_fetch_assoc() $row = mysql_fetch_row($res); $row = mysql_fetch_assoc($res); $row[0] = “1” $row[1] = $row[0] = “1” $row[1] = $row[“id”] = “1” $row[“ ”] = $row[“id”] = “1” $row[“ ”] =

MySQL Security Issues Many security issues inherited from external sources Database security particularly important when reading user input SQL Injection attacks common attacks with PHP $res = mysql_query("SELECT userid FROM teacher where name=\”$username\” AND password=\”$password\”"); But if $password is “ OR “1” = “1 The query becomes: SELECT userid FROM teacher where name=“name” AND password=“” OR “1” = “1” and the attacker will gain entry anyway! This is known as a SQL Injection attack!

Ways of securing PHP/SQL One of the easiest ways of preventing SQL Injection attacks is to escape the string first. This can be done using the mysql_real_escape_string function. This adds a \ to all quotes, so the parameter can’t interfere with the query. $user = $_POST[“username”]; $password = $_POST[“password”]; $user = mysql_real_escape_string($user); $password = mysql_real_escape_string($password); $res = mysql_query(“SELECT userid FROM users WHERE name=“$user” and password=“$password”); So “ OR “1” = “1 becomes \” OR \”1\” = \” 1 And the password is treated as a single string rather than MySQL syntax

Securing PHP with MYSQLi A better way is to use MySQLi, which is an improved version of the PHP MySQL driver MySQLi is still fairly new, so some hosts may not provide it yet MySQLi allows MySQL querys to be paramaterized. The queries are prepared without the parameters, and then inserted using the bind_param function The first parameter of the bind_param describes the number and type of arguments. For instance “si” tells the function to expect two arguments: a string and an integer

PHP and MySQLi $user = “me”; $password=“password”; $query->prepare(“SELECT userid from users where user=? and password=?); $query->bind_param(“ss”,$user,$password); $query->bind_result($userid); $query->execute(); $query->fetch(); User id is now stored in $userid

MD5 Security When storing passwords in a database, it is a good idea to hash them first This prevents people who may have access to the database from discovering the user’s password One of the most common hashes is MD5 (Message Digest) When user registers, the registration script takes an md5 hash of the password and stores the hash instead of the password When user logs in, the login script takes an md5 hash of the supplied password and checks it against the hash stored in the database If these two hashes are identical, the login script lets the person in

MD5 Example There are functions in both PHP and MySQL which calculate MD5 – this example uses both to demonstrate both Registration script: $user = $_GET[“user”]; $password = $_GET[“password”]; mysql_query(“INSERT INTO user (user, password) VALUES (“$user”, md5(“$password”))”) or die(“Error: “. mysql_error()); Login script: $user = $_GET[“user”]; $password = md5($_GET[“password”]); $res = “SELECT id FROM user WHERE user=“$user” AND password=“$password””;