SlideShare a Scribd company logo
1 of 17
Download to read offline
hasen@microcis.net July 12, 2013Hassen poreya
Trainer, Cresco Solution
Afghanistan Workforce
Development Program
PHP
Form Handling, and File Handling
Form Handling
 An HTML form is used to take the data from a user,
and then you can use those data that are held by
the form.
 Different inputs are available in a form that are
used to get input from a user.
 Text fields, text areas, selection lists, and radio
buttons, etc.
 When the user clicks the “submit” button, each of
these above becomes a string of text offered to the
Web server.
Note
 When a form is submitted, PHP turns each form
field into a variable.
 If you put two form variables on a page with the
same name, the second one may overwrite the
value of the first.
<form name=“login”
action=“index.php” method=“POST”>
</form>
GET
 The GET method is designed for retrieving
information, such as a document, an image, or the
results of a database query, from the server.
 The GET method is what a web browser uses when
the user types in a URL or clicks on a link.
POST
 The POST method is meant for posting confidential
information, such as a credit card numbers,
passwords or information that is to be stored in a
database, to the server.
 When the user submits a form, either the GET or POST
method can be used
Accessing Form variables
 $_GET, $_POST, $_REQUEST
 PHP super global associative arrays
 Hold information sent from an HTML form.
 PHP automatically places each form variable value
into:
 $_GET[form variable name],
 $_POST[form variable name],
 $_REQUEST[form variable name]
Accessing Form variables
 $_GET
 Hold information sent from an HTML form using GET
method: $_GET['var']
 $_POST
 Hold information sent from an HTML form using POST
method: $_POST['var']
 $_REQUEST
 The form data submitted using either POST or GET
method, are also available in $_REQUEST variables.
The Difference
 $_GET retrieves variables from the query string,
or your URL.
 $_POST retrieves variables from a POST method,
such as (generally) forms.
 $_REQUEST is a merging of $_GET and
$_POST where $_POST overrides $_GET.
 Good to use $_REQUEST on self referential
forms for validations. Hope that helps you out!
Example – info.php
<html>
<body>
<form action="welcome.php“
method="post">
Name: <input type="text"
name="name" />
Age: <input type="text"
name="age" />
<input type="submit" />
</form>
</body>
</html>
Example – welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?>years old.
</body>
</html>
Exercise
 Create a page that user can input First Name, Last
Name, Faculty, Age, etc. When a user clicks the
“Submit” button, the form data is sent to and
displayed on a different page. Use GET method, and
GET super global variable.
 Create a page that user input his name and birth
year, in another page his name should be printed
along with his age. Use GET method, and REQUEST
super global variable.
Attention!
 You should care about what a user is inputting in
your form field. You should always check user’s
inputs.
 There are some built-in functions that prevents
injections, and other kinds of attacks via from fields.
 Moreover, you can validate your HTML form with
Javascript as well as using PHP regular expression to
check what a user has entered.
 Empty fields shouldn’t be submitted. That’s why you
use isset() function to check whether a user
entered data into a form field or not.
File Handling
 After submitting a form, the data may be shown in
the screen, or inserted in a database or even stored
in a file.
 To store the data in a file you need to know some
functions listed below:
 Fopen(); -- open a file
 Fwrite(); -- write in a file
 Feof – represent end of the file
 Fgets(); -- get the strings out of a file
 Fgetc(); -- get the character out of a file
 Close(); -- close a file
File Opening modes
Modes Description
r Read only. Starts at the beginning of the file.
r+ Read/Write. Starts at the beginning of the file.
w Write only. Opens and clears the contents of file; or creates a new
file if it doesn't exist.
w+ Read/Write. Opens and clears the contents of file; or creates a new
file if it doesn't exist.
a Append. Opens and writes to the end of the file or creates a new file
if it doesn't exist.
a+ Read/Append. Preserves file content by writing to the end of the file.
x Write only. Creates a new file. Returns FALSE and an error if file
already exists.
x+ Read/Write. Creates a new file. Returns FALSE and an error if file
already exists.
Example
<?php
$file=fopen("welcome.txt", "rw") or exit("Unable
to open file!");
$write="Welcome to PHP file handling";
fwrite($file, $write);
?>
<?php
$file2=fopen("welcome.txt", "r");
while(!feof($file2)){
echo fgetc($file2). "<br />";
if (feof($file)) echo "End of file";
}
?>
Exercise
1. Create a user registration form, which takes
username and password from a user and store
them in a file. – register.php
2. Create a login interface – login.php
1. Username field
2. Password field
3. Login button
3. When a user enter the registered username, the
location should goes to another page “index.php”
header(“Location:index.php”);
hasen@microcis.net July 12, 2013Hassen poreya
Trainer, Cresco Solution
Any Questions!

More Related Content

What's hot

Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHPNisa Soomro
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHPNicole Ryan
 
[DSBW Spring 2010] Unit 10: XML and Web And beyond
[DSBW Spring 2010] Unit 10: XML and Web And beyond[DSBW Spring 2010] Unit 10: XML and Web And beyond
[DSBW Spring 2010] Unit 10: XML and Web And beyondCarles Farré
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaOXUS 20
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1monikadeshmane
 
Building Adaptive Payment Systems with YQL
Building Adaptive Payment Systems with YQLBuilding Adaptive Payment Systems with YQL
Building Adaptive Payment Systems with YQLJonathan LeBlanc
 
Data file operations in C++ Base
Data file operations in C++ BaseData file operations in C++ Base
Data file operations in C++ BasearJun_M
 
Xml For Dummies Chapter 14 Processing Xml it-slideshares.blogspot.com
Xml For Dummies   Chapter 14 Processing Xml it-slideshares.blogspot.comXml For Dummies   Chapter 14 Processing Xml it-slideshares.blogspot.com
Xml For Dummies Chapter 14 Processing Xml it-slideshares.blogspot.comphanleson
 

What's hot (20)

uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2
 
Php files
Php filesPhp files
Php files
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
php file uploading
php file uploadingphp file uploading
php file uploading
 
Files in php
Files in phpFiles in php
Files in php
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
Php File Upload
Php File UploadPhp File Upload
Php File Upload
 
12th computer-application-unit-8-study-material-english-medium
12th computer-application-unit-8-study-material-english-medium12th computer-application-unit-8-study-material-english-medium
12th computer-application-unit-8-study-material-english-medium
 
WEB PROGRAMMING
WEB PROGRAMMINGWEB PROGRAMMING
WEB PROGRAMMING
 
Uploading a file with php
Uploading a file with phpUploading a file with php
Uploading a file with php
 
[DSBW Spring 2010] Unit 10: XML and Web And beyond
[DSBW Spring 2010] Unit 10: XML and Web And beyond[DSBW Spring 2010] Unit 10: XML and Web And beyond
[DSBW Spring 2010] Unit 10: XML and Web And beyond
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
 
Full xml
Full xmlFull xml
Full xml
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
 
Html5 cheat sheet
Html5 cheat sheetHtml5 cheat sheet
Html5 cheat sheet
 
Building Adaptive Payment Systems with YQL
Building Adaptive Payment Systems with YQLBuilding Adaptive Payment Systems with YQL
Building Adaptive Payment Systems with YQL
 
Data file operations in C++ Base
Data file operations in C++ BaseData file operations in C++ Base
Data file operations in C++ Base
 
Xml For Dummies Chapter 14 Processing Xml it-slideshares.blogspot.com
Xml For Dummies   Chapter 14 Processing Xml it-slideshares.blogspot.comXml For Dummies   Chapter 14 Processing Xml it-slideshares.blogspot.com
Xml For Dummies Chapter 14 Processing Xml it-slideshares.blogspot.com
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 

Viewers also liked

Web app development_my_sql_08
Web app development_my_sql_08Web app development_my_sql_08
Web app development_my_sql_08Hassen Poreya
 
Web app development_html_css_03
Web app development_html_css_03Web app development_html_css_03
Web app development_html_css_03Hassen Poreya
 
Web app development_php_05
Web app development_php_05Web app development_php_05
Web app development_php_05Hassen Poreya
 
Learn to Code with JavaScript - Choose Your Own Adventures
Learn to Code with JavaScript - Choose Your Own AdventuresLearn to Code with JavaScript - Choose Your Own Adventures
Learn to Code with JavaScript - Choose Your Own AdventuresTessa Mero
 
Web app development_database_design_er-mapping_12
Web app development_database_design_er-mapping_12Web app development_database_design_er-mapping_12
Web app development_database_design_er-mapping_12Hassen Poreya
 
Web app development_database_design_11
Web app development_database_design_11Web app development_database_design_11
Web app development_database_design_11Hassen Poreya
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09Hassen Poreya
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04Hassen Poreya
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06Hassen Poreya
 
Web app development_database_design_10
Web app development_database_design_10Web app development_database_design_10
Web app development_database_design_10Hassen Poreya
 
Web app development_crud_13
Web app development_crud_13Web app development_crud_13
Web app development_crud_13Hassen Poreya
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Hassen Poreya
 
Web app development_html_02
Web app development_html_02Web app development_html_02
Web app development_html_02Hassen Poreya
 
Web app development_html_01
Web app development_html_01Web app development_html_01
Web app development_html_01Hassen Poreya
 

Viewers also liked (15)

Web app development_my_sql_08
Web app development_my_sql_08Web app development_my_sql_08
Web app development_my_sql_08
 
Web app development_html_css_03
Web app development_html_css_03Web app development_html_css_03
Web app development_html_css_03
 
Web app development_php_05
Web app development_php_05Web app development_php_05
Web app development_php_05
 
Learn to Code with JavaScript - Choose Your Own Adventures
Learn to Code with JavaScript - Choose Your Own AdventuresLearn to Code with JavaScript - Choose Your Own Adventures
Learn to Code with JavaScript - Choose Your Own Adventures
 
Web app development_database_design_er-mapping_12
Web app development_database_design_er-mapping_12Web app development_database_design_er-mapping_12
Web app development_database_design_er-mapping_12
 
Web app development_database_design_11
Web app development_database_design_11Web app development_database_design_11
Web app development_database_design_11
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
 
Web app development_database_design_10
Web app development_database_design_10Web app development_database_design_10
Web app development_database_design_10
 
CodeIgniter Practice
CodeIgniter PracticeCodeIgniter Practice
CodeIgniter Practice
 
Web app development_crud_13
Web app development_crud_13Web app development_crud_13
Web app development_crud_13
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14
 
Web app development_html_02
Web app development_html_02Web app development_html_02
Web app development_html_02
 
Web app development_html_01
Web app development_html_01Web app development_html_01
Web app development_html_01
 

Similar to Web app development_php_07

PHP fundamnetal in information technology CHapter -02.pptx
PHP fundamnetal in information technology CHapter -02.pptxPHP fundamnetal in information technology CHapter -02.pptx
PHP fundamnetal in information technology CHapter -02.pptxworldchannel
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Subhasis Nayak
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Gheyath M. Othman
 
Chapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxChapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxAhmedKafi7
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingAhmed Swilam
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql05 File Handling Upload Mysql
05 File Handling Upload MysqlGeshan Manandhar
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptxSherinRappai
 
J query post method in php
J query post method in phpJ query post method in php
J query post method in phpSamir Patel
 
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.pptElieNGOMSEU
 
PHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connectingPHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connectingPraveenHegde20
 
Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....Lalith86
 

Similar to Web app development_php_07 (20)

PHP fundamnetal in information technology CHapter -02.pptx
PHP fundamnetal in information technology CHapter -02.pptxPHP fundamnetal in information technology CHapter -02.pptx
PHP fundamnetal in information technology CHapter -02.pptx
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
PHP-Part4
PHP-Part4PHP-Part4
PHP-Part4
 
Introduction to php web programming - get and post
Introduction to php  web programming - get and postIntroduction to php  web programming - get and post
Introduction to php web programming - get and post
 
forms.pptx
forms.pptxforms.pptx
forms.pptx
 
Php advance
Php advancePhp advance
Php advance
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Chapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxChapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptx
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql05 File Handling Upload Mysql
05 File Handling Upload Mysql
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
J query post method in php
J query post method in phpJ query post method in php
J query post method in php
 
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.ppt
 
PHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connectingPHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connecting
 
Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....
 

Recently uploaded

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 

Recently uploaded (20)

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 

Web app development_php_07

  • 1. hasen@microcis.net July 12, 2013Hassen poreya Trainer, Cresco Solution Afghanistan Workforce Development Program PHP Form Handling, and File Handling
  • 2. Form Handling  An HTML form is used to take the data from a user, and then you can use those data that are held by the form.  Different inputs are available in a form that are used to get input from a user.  Text fields, text areas, selection lists, and radio buttons, etc.  When the user clicks the “submit” button, each of these above becomes a string of text offered to the Web server.
  • 3. Note  When a form is submitted, PHP turns each form field into a variable.  If you put two form variables on a page with the same name, the second one may overwrite the value of the first. <form name=“login” action=“index.php” method=“POST”> </form>
  • 4. GET  The GET method is designed for retrieving information, such as a document, an image, or the results of a database query, from the server.  The GET method is what a web browser uses when the user types in a URL or clicks on a link.
  • 5. POST  The POST method is meant for posting confidential information, such as a credit card numbers, passwords or information that is to be stored in a database, to the server.  When the user submits a form, either the GET or POST method can be used
  • 6. Accessing Form variables  $_GET, $_POST, $_REQUEST  PHP super global associative arrays  Hold information sent from an HTML form.  PHP automatically places each form variable value into:  $_GET[form variable name],  $_POST[form variable name],  $_REQUEST[form variable name]
  • 7. Accessing Form variables  $_GET  Hold information sent from an HTML form using GET method: $_GET['var']  $_POST  Hold information sent from an HTML form using POST method: $_POST['var']  $_REQUEST  The form data submitted using either POST or GET method, are also available in $_REQUEST variables.
  • 8. The Difference  $_GET retrieves variables from the query string, or your URL.  $_POST retrieves variables from a POST method, such as (generally) forms.  $_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET.  Good to use $_REQUEST on self referential forms for validations. Hope that helps you out!
  • 9. Example – info.php <html> <body> <form action="welcome.php“ method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
  • 10. Example – welcome.php <html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?>years old. </body> </html>
  • 11. Exercise  Create a page that user can input First Name, Last Name, Faculty, Age, etc. When a user clicks the “Submit” button, the form data is sent to and displayed on a different page. Use GET method, and GET super global variable.  Create a page that user input his name and birth year, in another page his name should be printed along with his age. Use GET method, and REQUEST super global variable.
  • 12. Attention!  You should care about what a user is inputting in your form field. You should always check user’s inputs.  There are some built-in functions that prevents injections, and other kinds of attacks via from fields.  Moreover, you can validate your HTML form with Javascript as well as using PHP regular expression to check what a user has entered.  Empty fields shouldn’t be submitted. That’s why you use isset() function to check whether a user entered data into a form field or not.
  • 13. File Handling  After submitting a form, the data may be shown in the screen, or inserted in a database or even stored in a file.  To store the data in a file you need to know some functions listed below:  Fopen(); -- open a file  Fwrite(); -- write in a file  Feof – represent end of the file  Fgets(); -- get the strings out of a file  Fgetc(); -- get the character out of a file  Close(); -- close a file
  • 14. File Opening modes Modes Description r Read only. Starts at the beginning of the file. r+ Read/Write. Starts at the beginning of the file. w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist. w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist. a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist. a+ Read/Append. Preserves file content by writing to the end of the file. x Write only. Creates a new file. Returns FALSE and an error if file already exists. x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists.
  • 15. Example <?php $file=fopen("welcome.txt", "rw") or exit("Unable to open file!"); $write="Welcome to PHP file handling"; fwrite($file, $write); ?> <?php $file2=fopen("welcome.txt", "r"); while(!feof($file2)){ echo fgetc($file2). "<br />"; if (feof($file)) echo "End of file"; } ?>
  • 16. Exercise 1. Create a user registration form, which takes username and password from a user and store them in a file. – register.php 2. Create a login interface – login.php 1. Username field 2. Password field 3. Login button 3. When a user enter the registered username, the location should goes to another page “index.php” header(“Location:index.php”);
  • 17. hasen@microcis.net July 12, 2013Hassen poreya Trainer, Cresco Solution Any Questions!