Intro To Php

  • Uploaded by: api-3844034
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Intro To Php as PDF for free.

More details

  • Words: 1,563
  • Pages: 80
Intro to PHP A brief overview – Sushil Kumar

What is PHP? 

PHP ("PHP: Hypertext Preprocessor") is a widely-used Open Source generalpurpose scripting language that is especially suited for Web development and can be embedded into HTML.



How it works PHP is installed on web server  Our web server is Apache  Server parses files based on extensions  Returns plain HTML, no code 

How To – The Basics Need to name files is a .php extension Example: index.php, mypage.php 

Open and close tags: Was:



Save file to server, view in a browser

Hello World helloworld.php

Variables Variables are like a cup The same cup can hold lots of different things Same with variables

Variables In PHP, you create a variable with a dollar sign and some text. Usually the text will be something descriptive of what it is going to hold. $name = “susheel”; $dept = “mca”; $local_addr = “kanpur”;

Variables $name = “Josiah”; $dob = “1/1/23”; $age = 84;

Conditional statement 

Like any other language php also use if else, for, while, switch case etc

If statements 

 $b) {     echo "a is bigger than b"; } else {     echo "a is NOT bigger than b"; } ?>

while loop

Include() Include function is used to include a file .  It can include any type of file. 

The syntax is

include “file name”;

Include()



Save it with hi.php

Include()

Functions Getting PHP to do some action for you



Functions 



Advance functions of php Date/time function

Date/time function D

l w d F

A textual representation of a day, three letters (mon to sun) The day of the week Numeric representation of the day of the week Day of the month(01 to 31) Name of the month (january to december )

Date/time function 

Try these

A,a,g,G,h,H,I,s,y ,Y Example:

E-mail 

One of the major uses of a server side scripting language is to provide a way of sending e-mail from the server and, in particular, to take form input and output it to an e-mail address. In this part I will show you how to send e-mail messages using PHP.

E-mail To send e-mail by php we use fuction mail() The syntax is:-



mail($to,$subject,$body,$headers);

E-mail 

$to = “[email protected]"; $subject = "PHP Is Great"; $body = "PHP is one of the best scripting languages around"; $headers = "From: [email protected]"; mail($to,$subject,$body,$headers); echo "Mail sent to $to";

Functions We’ll talk more about function later.

Passing information 

submit



Passing information method 2 

By session variable

session 

Session:- Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

session 

Start a session

session_start();

Session 

Store info in session

$_session[‘id’]=$id;

session 

Retrieving information from session

echo $_session[‘id’];

session 

Retrieving information from session

$id=$_session[‘id’]; echo $id;

session 

To register a session

session_register("id");

session Session_is_registered(id) Return true if register otherwise return false

session 

Destroy a session

session_destroy();

Implementation of session To implementation you must start session at each page where session is being used To session start session_start() is used .It must be type above the tag or the top of the page

Implementation of session 

If session has started successful you can store information in the session

Implementation of session An example to use session

Implementation of session

Implementation of session

Implementation of session 

Destroy a session



Redirect to a page Header function is used to redirect header("location:index.php");

Redirect to a page

Problem with session 

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at D:\mysite\page 1 s.php:3) in D:\mysite\page 1 s.php on line 7

references 

www.phpeasystep.com

A Basic Form (Design view)

A Basic Form (code view) How we do things with PHP: basicform.html


A Basic Form Weave HTML and PHP output.php

A Basic Form Capturing the data in output.php Variables: $_POST[‘name’]  $_POST[‘age’] 

Data Validation

We’ll talk more about validating user input later.

A Basic Form Outputting to the screen is nice, but boring We could email the results Let’s store data in a database

Layers of a Database Server  Database  Tables  Fields/Columns  Records  Data 

How to Get a Database 

Request a MySQL Database

Request a MySQL Database You will need: Server name  Database name  Username  Password 

Connecting to DB from PHP Create one connection script: dbconn.php
?>

Connecting to DB from PHP Create one connection script: dbconn.php
?>

Connecting to DB from PHP Remember, “Be Lazy!” At the top of each file that needs the DB:

Database Table Table named ‘info’ has two fields, name and age Use a SQL INSERT statement: $sql = “INSERT INTO info (name,age) values (‘$name’, ‘$age’)”;

Database Table Send it to the Database: mysql_query($sql);

The Whole Picture dbinsert.php
require(“dbconn.php”); $name = $_POST[‘name’]; $age = $_POST[‘age’]; $sql = “INSERT into info (name,age) values(‘$name’, ‘$age’);” mysql_query($sql); ?> Thank you, your name and age were received.

The Whole Picture - Fancier fancydbinsert.php

HTML REVISITED Create a table by html  use of    simple formating  Inclusion css 

Create an Output Page Connect to the Server  Do a query of the data  Programmatically write the data to a page  View the page in a browser  Let’s see how to do it 

Connect to the Server First, include our connection script:

Do a Query of the Data This time we use SELECT $sql = “SELECT name, age FROM info”; Or if you have many fields and want to be LAZY! $sql = “SELECT * from info”;

Programmatically Write the Data Here’s the only hard part: ”; } ?>
”; echo $table->name; echo “”; echo $table->age; echo “


Putting it All Together statuspage.php ”; } ?>
”; echo $table->name; echo “”; echo $table->age; echo “


I Hate Objects! If you don’t like using mysql_fetch_object: 

mysql_fetch_row($result)

Putting it All Together statuspage.php
”; } ?>
”; echo $table[0]; echo “”; echo $table[1]; echo “


Data Validation Very Important!  Without it, your site and all others can be hacked!  PHP makes it easier 

Data Validation Check that you’re getting what you expect  Check that you’re getting the length you expect  Don’t trust JavaScript  Do client side validation and server side validation 

Data Validation client side validation  Use java script Client side validation is not secure because some browser like opera can disable it

Data Validation 

Do client side validation because it can not be disabled by user

Data Validation Get what you expect to get  Don’t change it, give error message 

Example: (validinsert.php) Age, should be less than 110, and numeric. Reject anything else if(strlen($age)>3){ //error message } if(!is_int($age)){ //error message } if($age>110 || $age<18){ //error message }

Data Validation

ctype_alnum -- Check for  alphanumeric character(s)  ctype_alpha -- Check for alphabetic  character(s)  ctype_digit -- Check for numeric  character(s)

Data Validation Get the length you expect

Make sure the username is no longer than 8 if(strlen($username)>8)){ //error message }

Mysql-php function          

mysql_close -- Close MySQL connection mysql_connect -- Open a connection to a MySQL Server mysql_create_db --  mysql_db_name -- Get result data mysql_db_query -- Send a MySQL query mysql_drop_db -- Drop (delete) a MySQL database mysql_errno -- Returns the numerical value of the error message from previous MySQL operation mysql_error  mysql_fetch_object -- Fetch a result row as an object mysql_query -- Send a MySQL query

Thanks I think that’s enough [email protected]

Related Documents

Intro To Php
November 2019 16
Intro Php
May 2020 4
Intro Php
October 2019 11
Php Intro
May 2020 9
Php Scripting Intro
May 2020 6