You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Heredocs can be an enormous help when preparing HTML and SQL statements.
<?php$post = array(
'title' => 'How to Register a Business in Trinidad and Tobago',
'author' => 'Dwayne R. Crooks',
'body' => 'Step 1. Do a name search.',
'published-date' => '10-10-2012'
);
// common to see EOT or EOD, but you can call it whatever you want$email = <<<EOT<h1>{$post['title']}</h1><p>By: {$post['author']}</p><div>{$post['body']}</div>EOT; // N.B. Cannot end the file with a heredocecho$email;
// alternativelyextract($post);
$email = <<<EOT<h1>$title</h1><p>By: $author</p><div>$body</div>EOT; // N.B. Must be on it's own line with no spaces at the beginningecho$email;
?>
# Showing the databases
mysql> SHOW DATABASES;
# Creating a database
mysql> CREATE DATABASE blog;
# Using a specific database
mysql> USE blog;
# Showing the tables in the database
mysql> SHOW TABLES;
# Creating a table
mysql> CREATE TABLE users(
-> id INT AUTO_INCREMENT,
-> first_name varchar(50) NOT NULL,
-> last_name varchar(50) NOT NULL,
-> email_address varchar(100) NOT NULL,
-> PRIMARY KEY (id)
-> );
# Viewing the structure of a table, i.e. it's schema
mysql> DESCRIBE users;
# Selecting all rows from a table
mysql> SELECT * FROM users;
Generally we use sessions to store user specific data. Sessions are stored on the server. The PHP engine sets a cookie (a way to store little bits of information on the client) and stores a session ID (a really long, randomly generated, string that's difficult to guess). When the user requests a specific URL on the website that session ID cookie that we set will be sent back to the server at which point the PHP engine would validate it and then subsequently retrieve the data that's associated with that specific session.
Using Sessions
The first step when working with sessions is to call session_start. It's important that this occurs before any HTML is echoed out. This is because, session_start actually sets some HTTP headers. Once this is done we can start setting values via keys on the session superglobal $_SESSION.
When it's time to cleanup our sessions, we use session_destroy. Think of a session as a user specific life-cycle. You open the browser, you visit a webpage, you close the browser. That was a session. In this case, the session would automatically be destroyed. However, say you want to manually destroy the session, for e.g. as part of some logout functionality. Then, that's where session_destroy comes into play.
<?phpsession_start();
// do your stuff with sessions// cleanup after yourselfsession_destroy();
$_SESSION = array(); // or [] if using PHP 5.4+// may also need to delete the cookie being used to propagate the session ID?>