Join David Powers for an in-depth discussion in this video Connecting to the database, part of PHP: Managing Persistent Sessions.
- We've set up the database…for storing user credentials and session data…so now we need to connect to it.…PHP offers a choice of connection methods:…MySQL Improved or PDO.…I'm gonna use PDO because it's database-neutral.…This will allow you to adapt the code…to work with a database other than MySQL or MariaDB…although you might need to make some adjustments to the SQL…if your chosen database supports a different dialect.…In the Chapter 2, 02_03 folder,…you'll find a copy of db_connect.php.…
We'll use this to connect to the database.…It contains a try/catch structure.…This is necessary because PDO throws an exception…if the connection fails.…If you don't catch the exception,…the error message exposes your database credentials,…including the password.…The connection script needs to go inside the try block,…so we'll begin by creating a variable…to store our PDO object.…We'll call that simply db…and the new keyword and the PDO constructor…and the first argument to the PDO constructor…is a database source name or DSN.…
- Configuring sessions
- Inspecting how session data is stored
- Implementing persistent sessions
- Creating a SQL database to store session data
- Implementing the PHP SessionHandlerInterface
- Writing session data
- Closing and destroying sessions
- Creating an auto-login system with persistent sessions
- Creating persistent logins
- Checking out visitor credentials
- Authenticating users
- Logging out users selectively
Skill Level Intermediate
PHP: Accessing Databases with PDO and MySQLiwith David Powers3h 47m Intermediate
1. Sessions and Security
2. Preparing the Database
Connecting to the database5m 49s
3. Storing Session Data in MySQL
4. Creating an Autologin System
5. Testing the Autologin System
Final thoughts3m 9s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.