
pset7: C$50 Finance Tommy MacWilliam Permissions PHP pset7: C$50 Finance Registration Stock Quotes Buying Stocks Tommy MacWilliam Selling Stocks History [email protected] October 30, 2011 Today’s Music pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I Basshunter Stock Quotes I Angel in the Night Buying Stocks I Plane to Spain Selling Stocks I Boten Anna History I Ievas Polka Today pset7: C$50 Finance Tommy MacWilliam Permissions I permissions PHP I PHP Registration Stock Quotes I registration Buying Stocks I stock quotes Selling Stocks I buying stock History I selling stock I maintaining history public_html pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I mkdir ~/public_html Stock Quotes I cd ~/public_html Buying Stocks I git clone Selling Stocks History http://cdn.cs50.net/2011/fall/psets/7/pset7.git Permissions pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I chmod: change permissions of file or folder Stock Quotes Buying Stocks I chmod a+x ~ Selling Stocks I make ~ executable for all users History Permissions pset7: C$50 Finance Tommy MacWilliam Permissions PHP I chmod a+x ~ ~/public_html ~/public_html/pset7 Registration I Stock Quotes make home directory executable by everyone Buying Stocks I chmod a+r css/* images/* Selling Stocks I make all CSS and image files readable by everyone History I can’t execute an image as a program! Permissions pset7: C$50 Finance Tommy MacWilliam Permissions I chmod 644 file PHP Registration rwx 111 7 Stock Quotes rw 110 6 rx 101 5 Buying Stocks I r 100 4 Selling Stocks wx 011 3 History w 010 2 x 001 1 Permissions Cheat Sheet pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I folders: chmod 711 folder Stock Quotes I image, HTML, CSS, and Javascript files: chmod 644 Buying Stocks logo.png Selling Stocks History I PHP files: chmod 600 file.php Home Page pset7: C$50 Finance Tommy MacWilliam Permissions PHP I create new index.html Registration I don’t forget to chmod 644 index.html Stock Quotes Buying Stocks I make it pretty! Selling Stocks I visit http://localhost/~jharvard History I validate HTML with http://validator.w3c.org PHP in 10 Seconds pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I files end in .php Stock Quotes I code enclosed between <?php ?> Buying Stocks I variables start with $ Selling Stocks History I variables are weakly typed PHP in 10 Seconds pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration Stock Quotes Buying Stocks Selling Stocks History Distro Code pset7: C$50 Finance Tommy MacWilliam Permissions I index.php: homepage PHP I common.php: code run before each page loads Registration Stock Quotes I constants.php: database information Buying Stocks I helpers.php: helpful functions like lookup() Selling Stocks I stock.php: definition of Stock struct History I login.php: display login form I login2.php: process login login2.php pset7: C$50 Finance Tommy MacWilliam Permissions PHP I get username and password from $_POST Registration I look up user from database based on username Stock Quotes I compare hash of inputted password to hash of Buying Stocks Selling Stocks password in database History I if matching, remember the ID of the user in $_SESSION I $_SESSION data persists across pages SELECT pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I SELECT * FROM users WHERE username = "malan" Stock Quotes I get the data from every column Buying Stocks I in a table called users Selling Stocks I only if the username column has the value malan History Conditions pset7: C$50 Finance Tommy MacWilliam Permissions PHP I SELECT * FROM users WHERE username = "malan" OR Registration cash < 9000 Stock Quotes Buying Stocks I SELECT username, hash FROM users WHERE username Selling Stocks != "malan" History I SELECT COUNT(*) FROM users WHERE cash > 9000 Using SQL pset7: C$50 Finance Tommy MacWilliam I mysql_connect(DB_SERVER, DB_USERNAME, Permissions DB_PASSWORD): connect to database PHP Registration I mysql_select_db(DB_NAME): specify database to Stock Quotes operate within Buying Stocks I $query = "SELECT * FROM users": create database Selling Stocks query History I $result = mysql_query($query): run database query I $row = mysql_fetch_array($result): convert result to PHP associative array, one row at a time TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration 1. display form Stock Quotes 2. check if passwords match Buying Stocks 3. check if username already exists Selling Stocks History 4. add row to database <td>Password:</td> <td><input name="password" type="password"></td> </tr> Forms pset7: C$50 Finance Tommy MacWilliam Permissions I cp login.php register.php PHP I <form action="login2.php" method="post"> Registration I needs to send data to register2.php instead! Stock Quotes Buying Stocks I also need to create a new password2 field Selling Stocks History TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration 1. display form Stock Quotes 2. check if passwords match Buying Stocks 3. check if username already exists Selling Stocks History 4. add row to database Matching Passwords pset7: C$50 Finance Tommy MacWilliam Permissions I make sure $_POST["password"] and PHP $_POST["password2"] aren’t blank, else apologize Registration Stock Quotes I check out the function empty() Buying Stocks I make sure $_POST["password"] and Selling Stocks $_POST["password2"] are the same History I "tommy" == "tommy" in PHP I woohoo! TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration 1. display form Stock Quotes 2. check if passwords match Buying Stocks 3. check if username already exists Selling Stocks History 4. add row to database Duplicate Usernames pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I make sure $_POST["username"] isn’t blank, else Stock Quotes apologize Buying Stocks I mysql_query returns NULL on failure Selling Stocks History I username is UNIQUE in the database TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration 1. display form Stock Quotes 2. check if passwords match Buying Stocks 3. check if username already exists Selling Stocks History 4. add row to database INSERT pset7: C$50 Finance Tommy MacWilliam Permissions PHP I INSERT INTO users (username, hash, cash) VALUES Registration ("tommy", "$1$supersecret", 10000.00) Stock Quotes Buying Stocks I modify the users table Selling Stocks I new row will have values for username, hash, and cash History I values for row are "tommy", "$1$supersecret", and 10000.00 Hashing pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I when logging in, we use the crypt of a password Stock Quotes Buying Stocks I when INSERTing values, we need to use Selling Stocks crypt($_POST["password"]) History Logging In pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I new user should be logged in automatically! Stock Quotes Buying Stocks I what happens when a user logged in? Selling Stocks I $_SESSION History TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration 1. display form Stock Quotes 2. check if passwords match Buying Stocks 3. check if username already exists Selling Stocks History 4. add row to database TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration Stock Quotes I retrieve stock information Buying Stocks I display quote Selling Stocks History Form pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I need a form in quote.php that sends data to Stock Quotes quote2.php Buying Stocks Selling Stocks I single row: symbol the user wants to look up History lookup pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I $s = lookup("AAPL") Stock Quotes Buying Stocks I returns a Stock object (just like a C struct) Selling Stocks I defined in stock.php History Stock pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I $s->symbol == "AAPL" Stock Quotes Buying Stocks I $s->name == "Apple, Inc." Selling Stocks I $s->price == 404.95 History TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration Stock Quotes I retrieve stock information Buying Stocks I display quote Selling Stocks History Displaying Quote pset7: C$50 Finance Tommy MacWilliam Permissions PHP I quote.php prompts for symbol, quote2.php displays Registration result Stock Quotes I make sure quote is valid, else apologize Buying Stocks I print("Price: " . $s->price); Selling Stocks History I printf("Price: %d", $s->price); I printf("Price: {$s->price}"); TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration Stock Quotes I retrieve stock information Buying Stocks I display quote Selling Stocks History TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I create new table for portfolios Stock Quotes I prompt user for symbol and shares Buying Stocks I add stock to portfolio if possible Selling Stocks History I subtract cash Portfolio Table pset7: C$50 Finance Tommy MacWilliam Permissions PHP I no room in our users table for stock information! Registration I good thing a single database can have multiple tables Stock Quotes Buying Stocks I need to keep track of user, stock, and how many shares Selling Stocks I id VARCHAR History I symbol VARCHAR I shares INT Portfolio Table pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration Stock Quotes Buying Stocks Selling Stocks History TODO pset7: C$50 Finance Tommy MacWilliam Permissions PHP Registration I create new table for portfolios Stock Quotes I prompt user for symbol and shares Buying
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages54 Page
-
File Size-