Where to learn PHP?
This post is not for learning PHP. It is meant to show good Codeblocks that are in most PHP files. It should help you so if you google and see older codesnippets you will know what part to replace with newer safer code.
Receive POST / GET Data from Forms
If you are going to get html form data (POST / GET) make sure to grab data with filter_input (http://php.net/manual/de/function.filter-input.php) and filter_input_array (http://php.net/manual/de/function.filter-input.php) don’t just trust the data you get from POST/GET if you show the data back to a user or write it to a database. So basically never trust the data. That will help prevent cross site scripting (XSS). By “don’t trust” I mean that the user could manipulate the POST data. Also they can include Javascript snippets in it.
The Filters can be found here (http://php.net/manual/de/filter.constants.php).
Also you can grab POST and GET data this way by using “INPUT_POST” or “INPUT_GET”
ex.
//DON'T //instead of directly getting the data like this $weather = $_INPUT["weather"]; $number= $_INPUT["number"]; //DO //filter the data and "sanitize" it $weather = filter_input(INPUT_POST, 'weather', FILTER_SANITIZE_STRING); $number = filter_input(INPUT_POST, 'number', FILTER_SANITIZE_NUMBER_INT); // or $number = filter_input(INPUT_POST, 'number', FILTER_SANITIZE_NUMBER_FLOAT);
Database
you will use database connections and if you want to dive into php I suggest you look at mysql aswell. Just to give you a heads up: If you encounter a mysql object in php it’s old use mysqli (http://php.net/manual/de/book.mysqli.php) or look at PDO (http://php.net/manual/de/book.pdo.php) from the get go thats the object oriented approach to databaseconnections. Those 2 will prevent sqlinjection where the normal mysql databaseconnection will not.
//mysqli $db = new EasyMySQLi('host', 'user', 'pass', 'DB'); $ary = $db->querySingleRow('SELECT * FROM exampletbl WHERE id > ?', 158); echo 'Example-Value is'.$ary['val']; //PDO $pdo = new PDO('mysql:dbname=db_Name;localhost', 'user', 'db_password', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //select $stmt = $pdo->prepare('SELECT * FROM tbl_name WHERE description LIKE :weather'); $stmt->execute(array('weather' => $weather)); $getAllData = $stmt->fetchAll(); foreach ($getAllData as $key => $value) { $tempArray[0] = $value['table_column1']; $tempArray[1] = $value['table_column2']; $tempArray[2] = $value['table_column3']; $tempArray[3] = $value['table_column4']; } //input $stmt = $pdo->prepare('INSERT INTO `tbl_name`(`description`) VALUE(:weather)'); $stmt->execute(array('weather' => $weather)); $InsertID = $pdo->lastInsertID();
Good to know
trust me on this one. var_dump() (http://php.net/manual/de/function.var-dump.php) is your BEST friend. If you are not sure what a variable holds, throw it at var_dump and it will tell you everything you need to know about the content of that variable.
var_dump($yourvariable);
Thats the most basic information you need before starting to read / see things on the internet, good luck.
I don’t think php is bad and neither should you, don’t listen to everything you read on the internet 🙂
Archives
- November 2023
- December 2022
- November 2022
- February 2022
- November 2021
- October 2021
- September 2021
- July 2021
- April 2021
- March 2021
- February 2021
- January 2021
- September 2020
- July 2020
- April 2020
- March 2020
- February 2020
- December 2019
- November 2019
- October 2019
- August 2019
- June 2019
- February 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- May 2018
- March 2018
- February 2018
- December 2017
- November 2017
- September 2017
- July 2017
- June 2017
- April 2017
- February 2017
- January 2017
- October 2016
- September 2016
- July 2016
- May 2016
- April 2016
- March 2016
- August 2015
- July 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- October 2014
- April 2014
- March 2014
- February 2014
Calendar
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |