PHP for hacks
Upcoming SlideShare
Loading in...5
×
 

PHP for hacks

on

  • 6,842 views

PHP primer for hacking

PHP primer for hacking

Statistics

Views

Total Views
6,842
Views on SlideShare
6,780
Embed Views
62

Actions

Likes
0
Downloads
6
Comments
0

4 Embeds 62

http://mj89sp3sau2k7lj1eg3k40hkeppguj6j-a-sites-opensocial.googleusercontent.com 55
http://www.linkedin.com 5
https://si0.twimg.com 1
http://www.docshut.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

PHP for hacks PHP for hacks Presentation Transcript

  • PHP for hacks Tom Praison tpraison@yahoo-inc.com IIT-Delhi 16-Aug-2012
  • Sample Codes• The sample code is available for download https://github.com/tompraison/php_sample
  • What we need to learn?• Enough PHP to handle simple request• How to talk to backend data store using PHP• How to parse XML/JSON in PHP• How to generate JSON in PHP
  • What is PHP?• Server side language• Very easy to learn• Available on LAMP stack (Linux Apache Mysql PHP)• Does not require any special tools. Create a file with .php extension and your done.
  • How it works?
  • Getting Started• You need a local server with PHP enabled.• XAMPP for windows• MAMP for Mac OSx• Linux has it by default
  • Getting StartedCreate a file hello.php into htdocs and call it like thishttp://localhost:8888/hello.php <?php $school="iit-delhi"; echo "Hello, World $school"; ?>
  • Basic Syntax• PHP blocks start with <?php and end with ?> -• Every line of PHP has to end with a semicolon ";”• Variables in PHP start with a $• You print out content to the document in PHP with the echo command.• $school is variable and it can be printed out• You can jump in and out of PHP anywhere in the document. So if you intersperse PHP with HTML blocks, that is totally fine. For example:
  • Mix Match• You can mix and match HTML and PHP <?php $origin = Outer Space; $planet = Earth; $plan = 9; $sceneryType = "awful"; ?> <h1>Synopsis</h1><p>It was a peaceful time on planet <?php echo $planet;?> and people in the <?php echo $sceneryType;?> scenery were unaware of the diabolic plan <?php echo $plan;?> from <?php echo $origin;?> that will take their senses to the edge of what can be endured.</p> demo1.php
  • Displaying more complex data• You can define arrays in PHP using the array() method $lampstack = array(Linux,Apache,MySQL,PHP);• If you simply want to display a complex datatype like this in PHP for debugging you can use the print_r() command $lampstack = array(Linux,Apache,MySQL,PHP); print_r($lampstack); demo2.php
  • Arrays• Accessing arrays using index <ul> <?php $lampstack = array(Linux,Apache,MySQL,PHP); echo <li>Operating System:.$lampstack[0] . </li>; echo <li>Server: . $lampstack[1] . </li>; echo <li>Database: . $lampstack[2] . </li>; echo <li>Language: . $lampstack[3] . </li>; ?> </ul> demo3.php
  • Arrays• Iterating through arrays <ul> <?php $lampstack = array(Linux,Apache,MySQL,PHP); $labels = array(Operating System,Server,Database,Language); $length = sizeof($lampstack); for( $i = 0;$i < $length;$i++ ){ echo <li> . $labels[$i] . : . $lampstack[$i] . </li>; } ?> </ul> sizeof($array) - this will return the size of the array demo4.php
  • Associative Arrays• PHP has associative arrays with string keys<ul><?php$lampstack = array( Operating System => Linux, Server => Apache, Database => MySQL, Language => PHP);$length = sizeof($lampstack);$keys = array_keys($lampstack);for( $i = 0;$i < $length;$i++ ){ echo <li> . $keys[$i] . : . $lampstack[$keys[$i]] . </li>;}?></ul> demo5.php
  • Functions<?phpfunction renderList($array){ if( sizeof($array) > 0 ){ echo <ul>; foreach( $array as $key => $item ){ echo <li> . $key . : . $item . </li>; } echo </ul>; }}$lampstack = array( Operating System => Linux, Server => Apache, Database => MySQL, Language => PHP);renderList($lampstack);?> demo6.php
  • Interacting with the web - URL parameters<?php$name = Tom;// if there is no language defined, switch to Englishif( !isset($_GET[language]) ){ $welcome = Oh, hello there, ;}if( $_GET[language] == hindi ){ $welcome = Namastae, ;}switch($_GET[font]){ case small: $size = 80; break; case medium: $size = 100; break; case large: $size = 120; break; default: $size = 100; break;}echo <style>body{font-size: . $size . %;}</style>;echo <h1>.$welcome.$name.</h1>;?> demo7.php
  • Loading content from the web<?php // define the URL to load $url = http://cricket.yahoo.com/player-profile/Sachin-Tendulkar_2962; // start cURL $ch = curl_init(); // tell cURL what the URL is curl_setopt($ch, CURLOPT_URL, $url); // tell cURL that you want the data back from that URL curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // run cURL $output = curl_exec($ch); // end the cURL call (this also cleans up memory so it is // important) curl_close($ch); // display the output echo $output;?> demo8.php
  • Displaying XML content• Demo – Showing Twitter trends given a place – Displaying XML Content – Displaying JSONdemo9.php
  • Connecting to MySQL• Demo10.php from source code• Simple example to fetch data from DB
  • Further Reference http://www.php.net/ http://developer.yahoo.com http://isithackday.com/hackday- toolbox/phpforhacks/index.html http://www.slideshare.net/tompraisonhttps://github.com/tompraison/php_sample