PHP
Session , Cookies , Database Extensions
&
Abstract Layer
- Nishant Shrivastava
nishant@weboniselab.com
GitHub : nishant-shrivastava
Twitter - n1shant
Maintaining State
- HTTP is a State less protocol.
- Why maintaining state.
- Ways to maintain it over Web
- Session
- Cookie
Session
-
Session is the period of activity between a user logging in and logging out of a (multi-user) system.
OR
- Session is a semi-permanent interactive information interchange, also known as a dialogue, a conversation or a meeting, between two or more communicating devices, or between a computer and user.
Session...
- session_start() OR session_register()
- The Global Var : $_SESSION
- Run time Configuration
- $ php -i | grep session
- Setting Session
- $_SESSION['user_id'] = 'SomeId';
- Un-setting Session
- unset($_SESSION['user_is']);
- DO NOT ==> unset($_SESSION);
Cookies
!theBelowOne
Cookies
- Also known as HTTP cookie, web cookie, or browser cookie
- It is a small piece of data sent from a website and stored in a user's web browser.
-
PHP transparently supports HTTP cookies.
- Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users.
Cookies
- setcookie()
- Name
- Value
- Expire
- setrawcookie()
- The Global varriable : $_COOKIE
- Configuration
- $ php -i | grep cookie
DataBASE Extensions
Apache2 & Modules
- mysql
- mysqli
- pgsql
- mongo
Mysql / Mysqli
# MySQL Connect & Close : Till 5
<?php
$mysqlObj = mysql_connect('localhost' , 'mysql_user' , 'password');
if(!$mysqlObj) {
die('[' . mysql_errorno() . ']Could not connect to the DB : ' . mysql_error());
}
mysql_close($mysqlObj);
?>
# MySqli Connect & Close : > 5
<?php
$mysqli = new mysqli('localhost' , 'user' , 'password' , 'database');
if($mysqli->connect_errno) {
echo 'Failed to connect to Mysql : (' . $mysql->connect_errno . ' )' . $mysql->connect_error;
}
?>
Abstract Layer
The name is PDO,
PHP Data Objects
What..
-
Common interface to any number of database systems.
- Written in C, so you know it’s FAST!
- Designed to make use of all the PHP 5.1 features to simplify interface.
Need..
-
Many native database extensions that are similar but do not provide the same interface.
- In most cases, very old code that does not even scratch the surface of what PHP can offer.
- In many instances does not account for all the capabilities offered by the database.
Supported databases
-
MySQL 3,4,5 (depends on client libs)
- PostgreSQL
- SQLite 2 & 3
- ODBC
- DB2
- Oracle
- Firebird
- FreeTDS/Sybase/MSSQL
Using PDO
// MySQL connection $db = new PDO(‘mysql:host=localhost;dbname=testdb’, $login, $passwd); // PostgreSQL $db = new PDO(‘pgsql:host=localhost port=5432 dbname=testdb user=john password=mypass’); // SQLite $db = new PDO(‘sqlite:/path/to/database_file’);
// Handling Exceptions
<?php
try { $db = new PDO(…); } catch (PDOException $e) { echo $e->getMessage(); }
?>
Persistent connection
<?php
//Passing an argument for creating Persistent Connection.
$opt = array(PDO::ATTR_PERSISTENT => TRUE) ; try { $db = new PDO(“dsn”, $l, $p, $opt); } catch (PDOException $e) { echo $e->getMessage(); }
?>
Using PDO
- Direct Execution
- Prepared Statement
- Compile once, execute as many times as you want.
-
Clear separation between structure and input, which prevents SQL injection.
-
Often faster then query()/exec() even for single runs
CRUD
# Insert
<?php
$title = 'PHP Securities';
$author = "Rasmus Leodorf";
$sql = "INSERT INTO books (title,author) VALUES (:title,:author)";
$q = $db->prepare($sql);
$q->execute(array(':author' => $author , ':title' => $title));
?>
# Update
<?php
$title = 'PHP Pattern'; $author = 'Someone'; $id = 3;
$query = 'UPDATE books SET title=? , author=? WHERE id=?';
$q = $db->prepare($sql);
$q->execute(array($title , $author , $id));
?>
CRUD
# Fetching Data
$res = $db->query(“SELECT * FROM users”, PDO::FETCH_ASSOC);
foreach ($res as $row) { // $row == associated array representing // the row’s values. }
// Other way
$query = “SELECT * FROM users”; $res = $db->query($query)->fetchAll(PDO::FETCH_ASSOC);
Thanks..!
:)
PHP - Session , Cookie , Database extensions and PDO
By Nishant Shrivastava
PHP - Session , Cookie , Database extensions and PDO
- 1,217