This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Instead of: | |
<?php echo $name; ?> | |
Use: | |
<?php echo htmlspecialchars($name, ENT_NOQUOTES, 'UTF-8'); ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$dbh = new PDO('MySQL:host=localhost;dbname=example', | |
$username, $password, | |
array(PDO::MySQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); | |
} catch (PDOException $e) { | |
// You probably want to handle DB failure better than this: | |
die('Oh no, the database is dead!'); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php echo htmlspecialchars($my_var, ENT_COMPAT, 'UTF-8'); ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | |
"http://www.w3.org/TR/html4/strict.dtd"> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | |
<title>My example page</title> | |
</head> | |
… |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db = new PDO('mysql:host=hostname;dbname=defaultDbName', | |
'username', 'password', | |
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); | |
$names = array('Alice', 'Bob', 'Charlie'); | |
$values = array_map(array($db,'quote'),$names); | |
$query = 'SELECT * FROM my_table WHERE name IN ('.join(',',$values).')'; | |
$result = $db->query($query); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db = new PDO('mysql:host=hostname;dbname=defaultDbName', | |
'username', 'password', | |
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); | |
$query = 'SELECT * FROM my_table WHERE title = :title'; | |
$stmt = $db->prepare($query); | |
$stmt->bindValue(':title', $myTitle); | |
$stmt->execute(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$query = 'SELECT * FROM my_table WHERE title = :title'; | |
$stmt = $db->prepare($query); | |
$stmt->bindValue(':title', $title); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// See https://lazycat.org/php-curl.html for license & known issues | |
// P.S. You'd better have a very good reason for using this instead of http://guzzlephp.org/ | |
function httpGet($url, $ttl = 86400) | |
{ | |
/* Change this or make it an option as appropriate. If you're | |
* getting urls that shouldn't be visible to the public, put the | |
* cache folder somewhere it can't be accessed from the web | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$out = htmlspecialchars( | |
html_entity_decode($in, ENT_QUOTES, 'UTF-8'), | |
ENT_QUOTES, 'UTF-8' | |
); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* html_convert_entities($string) -- convert named HTML entities to | |
* XML-compatible numeric entities. | |
*/ | |
function html_convert_entities($string) { | |
return preg_replace_callback('/&([a-zA-Z][a-zA-Z0-9]+);/S', | |
'convert_entity', $string); | |
} |
NewerOlder