PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/phpforhacks/index.php

https://github.com/codepo8/hackday-toolbox
PHP | 321 lines | 278 code | 4 blank | 39 comment | 32 complexity | e6fe90899da10907e3ceb768cfcb5711 MD5 | raw file
  1. <?php if(!isset($_GET['demo'])){/*
  2. Simple PHP demo
  3. */?><?php
  4. $myname = 'Chris';
  5. echo '<p>This is PHP!</p>';
  6. echo "<p>My name is $myname</p>";
  7. echo '<p>My name in another notation is still '.$myname.'</p>';
  8. ?><?php }?><?php if($_GET['demo']=='1'){/*
  9. Showing the interaction between PHP and HTML
  10. */?><?php
  11. $origin = 'Outer Space';
  12. $planet = 'Earth';
  13. $plan = 9;
  14. $sceneryType = "awful";
  15. ?>
  16. <h1>Synopsis</h1>
  17. <p>It was a peaceful time on planet <?php echo $planet;?>
  18. and people in the <?php echo $sceneryType;?> scenery were unaware
  19. of the diabolic plan <?php echo $plan;?> from <?php echo $origin;?>
  20. that will take their senses to the edge of what can be endured.</p>
  21. <?php }?><?php if($_GET['demo']=='2'){/*
  22. Using print_r() for debugging
  23. */?><?php
  24. $lampstack = array('Linux','Apache','MySQL','PHP');
  25. print_r($lampstack);
  26. ?><?php }?><?php if($_GET['demo']=='3'){/*
  27. Accessing array members
  28. */?><ul><?php
  29. $lampstack = array('Linux','Apache','MySQL','PHP');
  30. echo '<li>Operating System:'.$lampstack[0] . '</li>';
  31. echo '<li>Server:' . $lampstack[1] . '</li>';
  32. echo '<li>Database:' . $lampstack[2] . '</li>';
  33. echo '<li>Language:' . $lampstack[3] . '</li>';
  34. ?></ul><?php }?><?php if($_GET['demo']=='4'){/*
  35. Looping over arrays
  36. */?><ul><?php
  37. $lampstack = array('Linux','Apache','MySQL','PHP');
  38. $labels = array('Operating System','Server','Database','Language');
  39. $length = sizeof($lampstack);
  40. for( $i = 0;$i < $length;$i++ ){
  41. echo '<li>' . $labels[$i] . ':' . $lampstack[$i] . '</li>';
  42. }
  43. ?></ul><?php }?><?php if($_GET['demo']=='5'){/*
  44. Using array keys and several arrays
  45. */?><ul><?php
  46. $lampstack = array(
  47. 'Operating System' => 'Linux',
  48. 'Server' => 'Apache',
  49. 'Database' => 'MySQL',
  50. 'Language' => 'PHP'
  51. );
  52. $length = sizeof($lampstack);
  53. $keys = array_keys($lampstack);
  54. for( $i = 0;$i < $length;$i++ ){
  55. echo '<li>' . $keys[$i] . ':' . $lampstack[$keys[$i]] . '</li>';
  56. }
  57. ?></ul><?php }?><?php if($_GET['demo']=='6'){/*
  58. Looping over an associative array
  59. */?><ul><?php
  60. $lampstack = array(
  61. 'Operating System' => 'Linux',
  62. 'Server' => 'Apache',
  63. 'Database' => 'MySQL',
  64. 'Language' => 'PHP'
  65. );
  66. foreach( $lampstack as $key => $stackelm ){
  67. echo '<li>' . $key . ':' . $stackelm . '</li>';
  68. }
  69. ?>
  70. </ul><?php }?><?php if($_GET['demo']=='7'){/*
  71. Using foreach
  72. */?><ul><?php
  73. $lampstack = array(
  74. 'Operating System' => 'Linux',
  75. 'Server' => 'Apache',
  76. 'Database' => 'MySQL',
  77. 'Language' => 'PHP'
  78. );
  79. if( sizeof($lampstack) > 0 ){
  80. foreach( $lampstack as $key => $stackelm ){
  81. echo '<li>' . $key . ':' . $stackelm . '</li>';
  82. }
  83. }
  84. ?>
  85. </ul>
  86. <?php }?><?php if($_GET['demo']=='8'){/*
  87. Writing a reusable function
  88. */?><?php
  89. function renderList($array){
  90. if( sizeof($array) > 0 ){
  91. echo '<ul>';
  92. foreach( $array as $key => $item ){
  93. echo '<li>' . $key . ':' . $item . '</li>';
  94. }
  95. echo '</ul>';
  96. }
  97. }
  98. $lampstack = array(
  99. 'Operating System' => 'Linux',
  100. 'Server' => 'Apache',
  101. 'Database' => 'MySQL',
  102. 'Language' => 'PHP'
  103. );
  104. renderList($lampstack);
  105. $awfulacting = array(
  106. 'Natalie Portman' => 'Star Wars',
  107. 'Arnold Schwarzenegger' => 'Batman and Robin',
  108. 'Keanu Reaves' => '*'
  109. );
  110. renderList($awfulacting);
  111. ?><?php }?><?php if($_GET['demo']=='9'){/*
  112. Using $_GET parameters
  113. */?><?php
  114. $name = 'Chris';
  115. // if there is no language defined, switch to English
  116. if( !isset($_GET['language']) ){
  117. $welcome = 'Oh, hello there, ';
  118. }
  119. if( $_GET['language'] == 'fr' )
  120. $welcome = 'Salut, ';
  121. switch($_GET['font']){
  122. case 'small':
  123. $size = 80;
  124. break;
  125. case 'medium':
  126. $size = 100;
  127. break;
  128. case 'large':
  129. $size = 120;
  130. break;
  131. default:
  132. $size = 100;
  133. break;
  134. }
  135. echo '<style>body{font-size:' . $size . '%;}</style>';
  136. echo '<h1>'.$welcome.$name.'</h1>';
  137. ?><?php }?><?php if($_GET['demo']=='10'){/*
  138. Parameter filtering
  139. */?><?php
  140. $search_html = filter_input(INPUT_GET, 's', FILTER_SANITIZE_SPECIAL_CHARS);
  141. $search_url = filter_input(INPUT_GET, 's', FILTER_SANITIZE_ENCODED);
  142. ?>
  143. <form action="index.php" method="get">
  144. <div>
  145. <label for="search">Search:</label>
  146. <input type="text" name="s" id="search"
  147. value="<?php echo $search_html;?>">
  148. <input type="hidden" name="demo" value="10">
  149. <input type="submit" value="Make it so">
  150. </div>
  151. </form>
  152. <?php
  153. if(isset($_GET['s'])){
  154. echo '<h2>You searched for '.$search_html.'</h2>';
  155. echo '<p><a href="index.php?search='.$search_url.'">Search again.</a></p>';
  156. }?><?php }?><?php if($_GET['demo']=='11'){/*
  157. Loading HTML
  158. */?><?php
  159. // define the URL to load
  160. $url = 'http://www.smashingmagazine.com';
  161. // start cURL
  162. $ch = curl_init();
  163. // tell cURL what the URL is
  164. curl_setopt($ch, CURLOPT_URL, $url);
  165. // tell cURL that you want the data back from that URL
  166. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  167. // run cURL
  168. $output = curl_exec($ch);
  169. // end the cURL call (this also cleans up memory so it is
  170. // important)
  171. curl_close($ch);
  172. // display the output
  173. echo $output;
  174. ?><?php }?><?php if($_GET['demo']=='12'){/*
  175. Loading and filtering HTML
  176. */?><?php
  177. // define the URL to load
  178. $url = 'http://www.smashingmagazine.com';
  179. // start cURL
  180. $ch = curl_init();
  181. // tell cURL what the URL is
  182. curl_setopt($ch, CURLOPT_URL, $url);
  183. // tell cURL that you want the data back from that URL
  184. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  185. // run cURL
  186. $output = curl_exec($ch);
  187. // end the cURL call (this also cleans up memory so it is
  188. // important)
  189. curl_close($ch);
  190. // if a filter parameter with the value links was sent
  191. if($_GET['filter'] == 'links'){
  192. // get all the links from the document and show them
  193. echo '<ul>';
  194. preg_match_all('/<a[^>]+>[^<\/a>]+<\/a>/msi',$output,$links);
  195. foreach($links[0] as $l){
  196. echo '<li>' . $l . '</li>';
  197. }
  198. echo'</ul>';
  199. // otherwise just show the page
  200. } else {
  201. echo $output;
  202. }
  203. ?><?php }?><?php if($_GET['demo']=='13'){/*
  204. Loading and displaying XML
  205. */?><?php
  206. $url = 'http://rss1.smashingmagazine.com/feed/';
  207. $ch = curl_init();
  208. curl_setopt($ch, CURLOPT_URL, $url);
  209. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  210. $output = curl_exec($ch);
  211. curl_close($ch);
  212. $data = simplexml_load_string($output);
  213. echo '<ul>';
  214. foreach($data->entry as $e){
  215. echo '<li><a href="'.$e->link[0]['href'].'">'.$e->title.'</a></li>';
  216. }
  217. echo '</ul>';
  218. ?><?php }?><?php if($_GET['demo']=='14'){/*
  219. JSON example
  220. */?><?php
  221. $json = '{
  222. "lampstack":
  223. {
  224. "operatingsystem":"Linux",
  225. "server":"Apache",
  226. "database":"MySQL",
  227. "language":"PHP"
  228. }
  229. }';
  230. print_r(json_decode($json));
  231. ?>
  232. <?php }?><?php if($_GET['demo']=='15'){/*
  233. JSON API example
  234. */?><pre><?php
  235. $url = 'http://search.twitter.com/trends.json';
  236. $ch = curl_init();
  237. curl_setopt($ch, CURLOPT_URL, $url);
  238. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  239. $output = curl_exec($ch);
  240. curl_close($ch);
  241. $data = json_decode($output);
  242. print_r($data);
  243. ?></pre>
  244. <?php }?><?php if($_GET['demo']=='16'){/*
  245. Format JSON API results example
  246. */?><?php
  247. $url = 'http://search.twitter.com/trends.json';
  248. $ch = curl_init();
  249. curl_setopt($ch, CURLOPT_URL, $url);
  250. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  251. $output = curl_exec($ch);
  252. curl_close($ch);
  253. $data = json_decode($output);
  254. echo '<h2>Twitter trending topics ('.$data->as_of.')</h2>';
  255. echo '<ul>';
  256. foreach ($data->trends as $t){
  257. echo '<li><a href="'.$t->url.'">'.$t->name.'</a></li>';
  258. }
  259. echo '</ul>';
  260. ?>
  261. <?php }?><?php if($_GET['demo']=='17'){/*
  262. Web search example
  263. */?><?php
  264. $search_html = filter_input(INPUT_GET, 's', FILTER_SANITIZE_SPECIAL_CHARS);
  265. $search_url = filter_input(INPUT_GET, 's', FILTER_SANITIZE_ENCODED);
  266. ?>
  267. <form action="index.php" method="get">
  268. <div>
  269. <label for="search">Search:</label>
  270. <input type="text" name="s" id="search"
  271. value="<?php echo $search_html;?>">
  272. <input type="hidden" name="demo" value="17">
  273. <input type="submit" value="Make it so">
  274. </div>
  275. </form>
  276. <?php
  277. if(isset($_GET['s'])){
  278. echo '<h2>You searched for '.$search_html.'</h2>';
  279. $yql = 'select * from search.web where query="'.$search_url.'"';
  280. $url = 'http://query.yahooapis.com/v1/public/yql?q='.
  281. urlencode($yql).'&format=json&diagnostics=false';
  282. $ch = curl_init();
  283. curl_setopt($ch, CURLOPT_URL, $url);
  284. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  285. $output = curl_exec($ch);
  286. curl_close($ch);
  287. $data = json_decode($output);
  288. echo '<ul>';
  289. foreach ($data->query->results->result as $r){
  290. echo '<li><h3><a href="'.$r->clickurl.'">'.$r->title.'</a></h3>'.
  291. '<p>'.$r->abstract.' <span>('.$r->dispurl.')</span></p></li>';
  292. }
  293. echo '</ul>';
  294. echo '<p><a href="index.php?search='.$search_url.'&demo=17">Search again.</a></p>';
  295. }
  296. ?>
  297. <?php }?><?php if($_GET['demo']=='18'){/*
  298. Loading XML and converting it to JavaScript
  299. */?><?php
  300. header('Content-type: text/javascript');
  301. $url = 'http://rss1.smashingmagazine.com/feed/';
  302. $ch = curl_init();
  303. curl_setopt($ch, CURLOPT_URL, $url);
  304. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  305. $output = curl_exec($ch);
  306. curl_close($ch);
  307. $data = simplexml_load_string($output);
  308. $data = json_encode($data);
  309. echo 'var smashingrss='.$data;
  310. ?>
  311. <?php }?><?php if($_GET['demo']=='19'){/*
  312. Including XML as JavaScript
  313. */?><?php
  314. echo '<script src="http://icant.co.uk/articles/phpforhacks/index.php?demo=18"></script>';
  315. echo '<script>alert(smashingrss.title);</script>';
  316. ?>
  317. <?php }?>