90 likes | 257 Views
PHP Part 3. Relational Databases. Consider the following table How would you select all of User 2’s comments?. Comments. SELECT Comment, UserID FROM Comments WHERE ID = 2. Relational Databases. Consider the following tables How would you select all of Jane’s comments?. Users. Comments.
E N D
Relational Databases • Consider the following table • How would you select all of User 2’s comments? Comments SELECT Comment, UserID FROM Comments WHERE ID = 2
Relational Databases • Consider the following tables • How would you select all of Jane’s comments? Users Comments SELECT Users.Name, Users.UserID, Comments.Comment, Comments.UserID FROM Users, Comments WHERE Users.UserID = Comments.UserID AND Users.UserID = 2
Relational Databases • Consider the following tables • How would you select all of Joe’s comments? Users Comments SELECT Users.Name, Users.ID, Comments.Comment, Comments.UserID FROM Users, Comments WHERE Users.ID = Comments.UserID AND Users.ID = 1
HTTP Cookies • Cookies are small files that the server embeds on the client’s computer • Cookies are sent with each HTTP request • Often used to identify a user • Cookies have • name – the name of the cookie, required • value – the value of the cookie, required • expiration – when the cookie expires, optional • path – scope of cookie, optional • domain – domain of the cookie, optional • secure – only send cookie over HTTPS, optional • HTTP-Only – only allow access by HTTP, optional
PHP Cookies • Setting a Cookie • setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); • string $name, name of the cookie • string $value, value of the cookie • int $expire, Unix timestamp time()+60*60*24 • string $path, path on server ‘/’ all paths • string $domain, ‘www.example.com’ • bool $secure • bool $httponly • Must be set before any output from the script • setcookie(“user”, “Fred”); • setcookie(“user”, “Cam”, time()+3600);
PHP Cookies • Getting a Cookie value • Use the $_COOKIE superglobal • $_COOKIE is an associative array of values if (isset($_COOKIE[“user”])) { echo “Welcome “ . $_COOKIE[“user”] . “<br/>”; } else { echo “Welcome guest <br/>”; }
PHP Cookie • Delete a Cookie • Set its expiration value to the past if(isset($_COOKIE[“user’])) { setcookie(“user”, “”, time-3600); }