PHP Programming Using Cloud 9 IDE.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Web Database Programming Connecting Database to Web.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Create an online booking system (login/registration)
INTERNET APPLICATION DEVELOPMENT For More visit:
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.
© 2003 By Default! A Free sample background from Slide 1 Week 2  Free PHP Hosting Setup  PHP Backend  Backend Security 
PHP meets MySQL.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Forms Collecting Data CSS Class 5. Forms Create a form Add text box Add labels Add check boxes and radio buttons Build a drop-down list Group drop-down.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
+ FORMS HTML forms are used to pass data to a server. begins and ends a form Forms are made up of input elements Every input element has a name and value.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 1: Introduction to IS2803 Rob Gleasure
MySQL MySQL and PHP – interacting with a database.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
Higher Computing Science Coding the Web: HTML, JavaScript, PHP and MySQL.
PHP Form Processing * referenced from
Learning Aim C.  In this section we will look at how text, tables, forms and frames can be used in web pages.
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,
Building Your Own Website Using:. Install & configure LAMP. Download WordPress and run it as a local website on your Raspberry Pi. Configure WordPress.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
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.
A pache M ySQL P hp Robert Mudge Reference:
Web Systems & Technologies
PHP (Session 2) INFO 257 Supplement.
PHP Built-In Functions
Intro to WordPress (Using XAMPP)
CHAPTER 5 SERVER SIDE SCRIPTING
Introduction to Dynamic Web Programming
IS1500: Introduction to Web Development
Loops BIS1523 – Lecture 10.
PHP: Inserting data FdSc Module 109 Server side scripting and
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Web Design and Development
Introduction to Web programming
PHP & MySQL Introduction.
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Arrays and files BIS1523 – Lecture 15.
PHP / MySQL Introduction
BASIC PHP and MYSQL Edward S. Flores.
PHP Introduction.
Website Development Basics with PHP MySQL
Intro to PHP & Variables
PHP Overview PHP: Hypertext Preprocessor Server-Side Scripting
Cookies BIS1523 – Lecture 23.
HTML Forms and User Input
ISC440: Web Programming 2 Server-side Scripting PHP 3
Introduction to Web programming
MySQL Web Application Connecting to a MySQL database
CIS 388 Internet Programming
Building Web Applications
Creating Forms on a Web Page
Tutorial 6 PHP & MySQL Li Xu
MySQL Web Application Connecting to a MySQL database
PHP Forms and Databases.
PHP-II.
Introduction to Web programming
Conection
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.
Presentation transcript:

PHP Programming Using Cloud 9 IDE

Web development LAMP stack Linux Apache MySQL PHP

PHP Server based scripting Open source Used in many websites Can be used within HTML code in pages Syntax: Case sensitive Semi colons at end of lines Curly brackets enclose blocks Variables start with $ - eg $myname Php embedded into HTML using <?php …?> tags

IDE Can use a text editor or Dreamweaver Must run on a server to get PHP to work Can buy your own hosting Or set up a “Localhost” to run locally (or off a USB stick) We use cloud 9 a free hosting service with an inbuilt IDE Mrs James can invite you to a free teamaccount

Syntax rules Similar to Javascript: Unique to PHP Semicolons at the end of lines Blocks of code are contained in {} Case sensitive Comments are /* */ Unique to PHP Php is embedded within the HTML using the following tags <?php ………?> Variables are declared with $ before their names Eg $yourname, $score, $username Concatentate text with a . (not a + sign) Output information to the web page using the function echo echo “hello world”

Php function echo will print data to the web page. Note: It is embedded within HTML tags

Reasons to use PHP Even for static sites that don’t require a backend database PHP can be useful as it allows you to share (or include) files This means you can use the same header or footer across multiple pages Or you can store commonly used functions in one place and refer to them across all pages

Structuring your PHP project Good developing practice to separate your code into logical groups - included files -images - css -javascript So it’s a good idea to create these folders now

Includes folder Create the following blank php files in your includes folder Header.php to hold all the code you want to appear at the top of every page Footer.php to hold the code to appear at the bottom of every page Functions.php which will hold all commonly used functions DBConnect.php which will hold the connection to the MySQL database

The HomePage – index.php All websites should have a homepage – if not the website will direct you to a list of directories – could be a security risk Usually they are called index.html or index.php Rename the hello-world.php to index.php and try opening your project again

Including files Use the include function to insert other php files into your web page

Header & Footer start Notice how the <html> tags and the body tags are split across the two included files

CSS allows you to do MUCH more with web pages – however it shouldn’t be the main focus of your project as its more style than algorithms! Add a stylesheet

Adding some input fields <?php include("includes/header.php")?><h1>Login</h1><p>Please login to proceed</p><form> <label>Username</label> <input type="text" name="username" placeholder="Enter your username"> <label>Password</label> <input type="password" name="pwd"> <br> <input type="submit" value="Login"></form><?php include("includes/footer.php")?> Nothing happens at the moment but you can see that there are 2 fields and a button

The <form> tag This marks out a portion of the web page to be acted upon. It can have several attributes Method = post or get (get can be a security risk) Action = the web page it should go to If blank the form will submit the data to the same page

Accessing the information typed in in a form Use the $_POST[] array to see what variables have been submitted So we can get what they typed in the Username field by using $_POST[“username”]

Simple authentication script Tests if the username & the password match given values and prints out a message

Issues with this code Invalid name or password appears for the first time even before the user has tried to login On correct login it should redirect to a different page Should there be different messages of the name and/or password are blank (validation)? The name & password are hardcoded – what happens if we want to add more valid logins Need to use a database! (more later)

Extra checks for the name & password Improvement 1 Extra checks for the name & password This if statement checks if the form has been submitted and stops the error msg appearing the 1st time you go to the page

Redirecting to a new page You will now need to create a welcome.php page of course (just copy index.php and then change the text inside) In practice any PHP that uses this redirect should be done BEFORE any HTML has been output to the screen (but this seems to work for now)

MySQL MySQL Database MySQL is the database system we will use to store any website information we need Eg User details for login Data that will be added from the website (eg sports results, reminders) Visitor information for tracking # start MySQL. Will create an empty database on first start $ mysql-ctl start # stop MySQL $ mysql-ctl stop # run the MySQL interactive shell $ mysql-ctl cli

PhpMyadmin Phpmyadmin is an easy to use PHP based Database Management System allowing you to create, edit and manage MySQL databases First, create a PHP workspace so you have PHP, MySQL, and Apache installed right away. You can then make sure you have MySQL installed by running: mysql-ctl install Then install phpMyAdmin: phpmyadmin-ctl install After the installation is complete you'll just want to make sure mysql is running once more: mysql-ctl start

No password by default! Security risk!

Create a database called users with 3 columns <?php // A simple PHP script demonstrating how to connect to MySQL. // Press the 'Run' button on the top to start the web server, // then click the URL that is emitted to the Output tab of the console. $servername = getenv('IP'); $username = getenv('C9_USER'); $password = ""; $database = "c9"; $dbport = 3306; // Create connection $db = new mysqli($servername, $username, $password, $database, $dbport); // Check connection if ($db->connect_error) { die("Connection failed: " . $db->connect_error); } e cho "Connected successfully (".$db->host_info.")";

Created – you can see the new table users in the list

This will manually add two users to the database (Don’t fill in the ID as MySQL will add that automatically)

See how the primary key ( the users_id field) is automatically added and incremented each time

Check you can connect to the database Add this code to your dbconnect.php file <?php $servername = getenv('IP'); $username = getenv('C9_USER'); $password = ""; $database = "c9"; // Create connection $db = new mysqli($servername, $username, $password, $database); // Check connection if ($db->connect_error) { die("Connection failed: " . $db->connect_error); } echo "Connected successfully (".$db->host_info.")"; ?> Test it by going directly to the dbconnect.php in the address bar – you should see this…

Using the dbconnect.php file in all pages Database credentials and the connection string should be just stored in one place If you store them in multiple places there will be an issue in finding them all if you need to update a password We now need to include this file in all our pages (as most pages will need to connect to the database in some way)

Getting Data Out of a database Use and SQL statement such as SELECT * from questions Or SELECT * from users WHERE userid=“mrsjames” Work out what the SQL statement you want to runi s first – test it in phpmyadmin

Using the mySQLi object in PHP Runs the SQL query and puts values in a variable called $result If there are some records returned… <?php $sql ="SELECT * from users WHERE username ='".$_POST["username"]."'";echo $sql;$result = $db->query($sql);if($result->num_rows > 0){ //echo "something found"; while ($row = $result->fetch_assoc()){ echo $row["username"]." - ".$row["password"]; }}if($_SERVER['REQUEST_METHOD']=="POST"){ if($_POST["username"]=="laura" && $_POST["pwd"]=="cake" ){ //echo "<h2>Welcome back Mrs James</h2>"; header("Location:page1.php"); }else{ echo "<h2>incorrect name & password</h2>"; }}?><h1>Login</h1><p>Please login to proceed</p><form action="" method="POST"> <label>Username</label> <input type="text" name="username" placeholder="Enter your username"> <br> <label>Password</label> <input type="password" name="pwd"> <br> <input type="submit" value="Login"></form><?php include("footer.php")?> Loops through each record checking the password matches