-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_cheatsheet2.php
270 lines (200 loc) · 6.75 KB
/
php_cheatsheet2.php
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/* written by Nick Shin - [email protected]
* the code found in this file is licensed under:
* - Unlicense - http://unlicense.org/
*
* this file is from https://github.com/nickshin/CheatSheets/
*
*
* this file contains some of my most used php snipets
* - user agent detection
* - crafted GET/POST headers
* - memcache
* - NoSQL
*
*
* best viewed in editor with tab stops set to 4
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
# ================================================================================
# user agent detection {{{
# ================================================================================
# redirect to mobile friendly crafted pages on first time visit
# but, make sure there's an option (in javascript) for 'view full site'
# version and vice versa ('view mobile site')
# NOTE: this is FIRST PASS -- make sure to do another check client side
# NOTE: this is FIRST PASS -- make sure to do another check client side
# NOTE: this is FIRST PASS -- make sure to do another check client side
$isMobile = 0;
# DIY:
$ua = $_SERVER['HTTP_USER_AGENT'];
if ( preg_match( '/iOS|Android|Mobile|Phone|Opera Mini/i', $ua ) )
$isMobile = 1;
# if using https://github.com/serbanghita/Mobile-Detect
# include 'Mobile_Detect.php';
# $detect = new Mobile_Detect();
# $isMobile = $detect->isMobile();
# in 'view full site'
# ----------------------------------------
if ( $isMobile ) {
if ( ! isset( $_COOKIE['mobile'] ) # first time here
|| $_COOKIE['mobile'] == 'y' ) # yes, view mobile version
{
header('Location: http://m.example.com/');
exit;
}
}
setcookie("mobile","n", time()+3600, "/","example.com");
# in 'view mobile site'
# ----------------------------------------
setcookie("mobile","y", time()+3600, "/","example.com");
# ================================================================================
# user agent detection }}}
# crafted headers {{{
# ================================================================================
# http://php.net/manual/en/function.header.php
#
# LOOK HERE for more header() options:
# - http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses
# WARNING: THESE FUNCTION MUST BE CALLED BEFORE ANY OUTPUT (including newlines)
# redirect
header('Location: http://m.example.com/');
exit;
# another redirect - NON standard: but supported by most browsers
header('Refresh: 5; url=http://m.example.com/');
# 404 normal
header("HTTP/1.0 404 Not Found");
# 404 FastCGI
header("Status: 404 Not Found");
# ----------------------------------------
# download dialog
# ...............
# outputting a PDF, suggest filename as "downloaded.pdf" and datadump 'original.pdf'
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
exit;
# ----------------------------------------
# many proxies and clients can be forced to disable caching with
header("Cache-Control: no-cache, must-revalidate"); # HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); # Date in the past
# ================================================================================
# crafted headers }}}
# memcache {{{
# ================================================================================
# nice PHP PDO SQLite3 example at:
# http://www.if-not-true-then-false.com/2012/php-pdo-sqlite3-example/
#
# and don't forget:
# http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html
# ! WATCH OUT ! some SQL statements will NOT work with some DB drivers
# for example, 'SHOW TABLES' will not work on sqlite
function db_setup()
#function db_setup( $hostname, $username, $password, $dbname )
{
try {
$dbh = new PDO("sqlite::memory:");
# $dbh = new PDO("sqlite:/path/to/database.sdb");
# $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
# $dbh = new PDO("pgsql:dbname=$dbname;host=$hostname", $username, $password );
# $dbh = new PDO('odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\accounts.mdb;Uid=Admin');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec("CREATE TABLE animals (
animal_id INTEGER PRIMARY KEY,
animal_type TEXT,
animal_name TEXT
)");
return $dbh;
} catch(PDOException $e)
{ echo $e->getMessage(); }
return null;
}
function db_warmup( $dbh, $dbname )
{
echo "\nwarming up DB\n";
$insert = "INSERT INTO $dbname(animal_type, animal_name) VALUES ('kiwi', 'troy')";
$count = $dbh->exec( $insert );
echo "DB insert results: $count\n"; # should be 1
}
function memcache_setup($qry)
{
echo "\nmcsetup\n";
$mc = new Memcache;
echo "good, looks like Memcache works with your PHP setup\n"; # else install memcache for your system and for php
$mc->connect( '127.0.0.1', 11211 ) or die ( 'unable to connect to memcached' );
# comment out the delete and rerun this code -- you should see difference immediately
$id = md5($qry);
$mc->delete($id);
return $mc;
}
function findValue( $qry, $dbh, $mc )
{
echo "\nfetching\n";
$id = md5($qry);
if ( $result = $mc->get($id) ) {
echo "memcached: $id\n";
echo "$result\n";
return;
}
echo "DB lookup (was NOT cached)\n";
$result = '';
$results = $dbh->query( $qry );
foreach( $results as $row )
$result .= $row['animal_type'] .' - '. $row['animal_name'];
echo "$result\n";
# now, cache it
$mc->set( $id, $result );
}
function test()
{
$dbh = db_setup();
$dbname = 'animals';
db_warmup( $dbh, $dbname );
$qry = "SELECT * FROM $dbname";
$mc = memcache_setup($qry);
$result = findValue( $qry, $dbh, $mc );
echo "\nagain\n";
$result = findValue( $qry, $dbh, $mc );
# done with test, house keeping...
$dbh->exec("DROP TABLE animals");
$dbh = null; # close the database connection
}
echo "<pre>\n";
test();
echo "</pre>\n";
# ================================================================================
# memcache }}}
# NoSQL {{{
# ================================================================================
/*
http://nosql-database.org/
http://ontwik.com/php/nosql-databases-what-why-and-when-lorenzo-alberton/
what to look for:
* map/reduce
* multi dimensions
* construct different ways to store data
o replication (redundancy(alternative version)/precalculated/secondary sets/etc.)
o disk space is cheaper and more scalable than CPU
k-v
o redis
o couchbase
column
o google: big table
o multi dimensional sorted map
- row_key
- column_key
- timestamp
o hadoop/hbase
o cassandra
document
o mongoDB
- http://www.phpclasses.org/blog/post/118-Developing-scalable-PHP-applications-using-MongoDB.html
o couchDB
graph
o new4j
coming soon, my most used snippets...
*/
# ================================================================================
# NoSQL }}}
# ================================================================================
?>