PHP Programming Exercises
- Write a php program to compare between things that are not integers
- Write a division table program in PHP using for loop
- Write a program in PHP to print prime numbers between 1 and 100
- Write a php program to print numbers from 10 to 1 using the recursion function
- Write a php program to store the username in cookie and check whether the user have successfully login or not
- Write a php program to convert the given string into an array
- Write a php program to loop over the json data
- Write a program in PHP to remove all html tags except paragraph and italics tags
- Write a program to loop through an associative array using foreach() or with each()
- Write a php program to differentiate between fgets, fgetss and fgetcsv
- There are two deals of an item to buy. The quantities and prices of the item are given below. Write a program in PHP to find the best deal to purchase the item
- Write a php program to set session on successful login
- Write a program in PHP to read from directory
- PHP create image from text and save
- How to get data from XML file in PHP
- PHP Create Word Document from HTML
- How to check whether a year is a leap year or not in PHP
- Fibonacci Series Program in PHP
- How to generate QR Code in PHP
- How does PHP store data in cache?
- How to detect a mobile device using PHP?
- How to send HTML form data to email using PHP?
- How to get location from IP address using PHP?
- How to lock a file using PHP?
- How to import a CSV file into MySQL using PHP
- How to fetch data from database in PHP and display in PDF
- How to insert image in database using PHP
- How to remove last character from string using PHP?
- Write a PHP program to reverse a string without predefined function
- Write a PHP program to calculate percentage of total
- How to sanitize input for MySQL using PHP?
- Write a program to calculate electricity bill in PHP
- How to send email with SMTP in PHP?
- How to Send Text Messages With PHP?
- How to convert stdClass object to Array in PHP?
- How do I import Excel data into MySQL database using PHP?
- How can I post a form without refreshing the page?
- How to sort table columns with PHP and MySQL?
- How to get current directory, filename and code line number in PHP
SQL Injection 1=1
Suppose there is a table in the database name 'company' and 'cmp_name' one of its fields. At the front end, there are some search modules that select company information on the basis of company name. In the controller, for the most part we compose the query to fetch the searched company name as-
$query = "SELECT * FROM company WHERE cmp_name = '$cmpname' ";
Suppose the attacker goes to this search module in the front end and instead of the company name, he has given the below code in the company name variable as -
OR '1' = '1'
At this point the select query becomes -
$query = "SELECT * FROM company WHERE cmp_name = '$cmpname' OR '1' = '1' ";
AS '1' = '1' condition is always evaluated to be true and executed, fetching all the data from the company table. By this way, the attacker can fetch all the company data. Therefore, to protect the database from attackers, it is important to filter and sanitize the client entered information prior to sending it to the database.
PHP provides different variables for sanitizing data. For example, passing in FILTER_SANITIZE_EMAIL will remove characters that are inappropriate for an email address to contain. That said, it does not validate the data. These are some examples of data sanitised variables-
PHP Sanitize Email
The PHP variable FILTER_SANITIZE_EMAIL is used to sanitize the email. It removes all illegal characters except letters, digits and !#$%&'*+-=?^_`{|}~@.[] and then checks whether the format is valid or not.
Example -
Output of the above code -
As, you can see in the above example, email is stored in the $email variable and it is sanitized using filter_var() to remove any illegal characters. After this process, the given email is validated.
PHP Sanitize String
The PHP variable FILTER_SANITIZE_STRING is used to sanitize the string. It strips all the HTML tags detected from a string.
Output of the above code -
Welcome to ETUTORIALSPOINT
In the given example, the variable $str contains a string. This string is sanitized using the string filter FILTER_SANITIZE_STRING to strip all the HTML tags. After this process, the given string is validated.
PHP Sanitize URL
The PHP constant FILTER_SANITIZE_URL removes all characters except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&= from the URL string and then check whether the format is valid or not.
Output of the above code -
https://www.etutorialspoint.com is valid
PHP Sanitize Input
The PHP FILTER_SANITIZE_ENCODED constant is used to remove or encode special characters in a URL.
Output of the above code -
www.etutorialspoint.com
PHP Sanitize Number Input
The PHP FILTER_SANITIZE_NUMBER_INT constant removes all characters except digits, plus and minus signs.
Output of the above code -
need an explanation for this answer? contact us directly to get an explanation for this answerE:\wamp\www\test\index.php:4:string '2-5+1' (length=5)