Learning guide
Practising PHP and MySQL? Optional persistent login with secure token storage. We focus on PHP Remember Me Cookie here.
Configuration belongs in config.php (PHP) or a single app.js entry (browser labs)—not sprinkled through every page.
Read a section, predict what should happen, then run it. Logs and the browser network tab are part of the lesson.
If local setup is new to you, block an hour once for Apache/MySQL or Live Server, then reuse that stack for other guides.
Start from index.php when you open the reference tree. The optional package at the bottom matches this article.
Concepts you should understand
Authentication labs cover identity, sessions, and password hashing. Upload labs cover validation, storage layout, and metadata. AJAX labs cover HTTP verbs, JSON contracts, and separating view from API. Map each file in the reference section to one of these ideas.
Optional local lab setup
PHP labs assume a local MySQL schema imported from the teaching database.sql file. These steps describe a learning environment, not commercial software installation.
- Create a folder under your local web root named
php-remember-mefor this exercise. - Start Apache and MySQL services on your machine (only needed for PHP/MySQL lessons).
- Import the bundled
database.sqlthrough phpMyAdmin to create practice tables. - Adjust
config.phpcredentials to match your local database user. - Open
http://localhost/php-remember-me/index.phpand step through each screen while reading the source. - Credentials for sandbox testing, if any, are documented only inside the private
README.txtshipped with the practice archive—not on this public page.
Guided walkthrough
Begin by using the feature as a learner: submit empty forms, invalid emails, and edge cases.
Watch how the UI responds, then locate the PHP or JavaScript responsible for that message.
Trace variables from $_POST or fetch bodies down to SQL or DOM updates.
Rewrite one branch in your own words—rename fields, add comments, or log values with error_log or console.debug.
If behavior changes unexpectedly, you have found a learning moment about sessions, scope, or async timing.
For topics like PHP Remember Me Cookie, compare your work against the annotated snippets below before peeking at the full reference tree. Spaced repetition (revisit the lesson days later) cements syntax into long-term memory better than a single rushed pass.
Annotated code samples
Each block is meant to be edited, not pasted blindly. Read the note under it, then try one small change.
Shared bootstrap for PHP Remember Me Cookie
<?php
require __DIR__ . '/config.php';
// Use db() for mysqli connectionCentral configuration avoids repeating host, user, and database name in every script. A helper like db() returns a single connection per request, which is easier to test and secure. Keep credentials out of version control and use different values on staging versus production.
If the sample uses a helper you do not recognise, look it up on PHP.net or MDN before copying it wholesale.
Prepared statement pattern
<?php
require __DIR__ . '/config.php';
$stmt = db()->prepare('SELECT id, name FROM users WHERE email = ? LIMIT 1');
$stmt->bind_param('s', $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();require loads shared database settings so every script uses one connection helper. The prepared statement uses a ? placeholder so user input is never concatenated into SQL. bind_param attaches the email string safely, and fetch_assoc returns one row as an associative array for login checks.
Prepared statements separate SQL structure from user data, which closes the most common SQL injection holes taught in security courses. Even when you trust your own form today, future features (search boxes, admin filters) often reuse the same query patterns—building them safely from day one prevents expensive refactors.
Jot down which line validates input and which line talks to the database—you will reuse that split in other labs.
Redirect after a successful action
<?php
header('Location: index.php');
exit;Post/Redirect/Get prevents duplicate form submissions when users refresh. Always call exit after header('Location: …') so no extra output is sent. Use relative paths when possible to survive domain changes.
Rename one function in your copy so you are sure you understand what it returns.
Security notes for students
Practice code deliberately simplifies reality. Before any production deployment, add HTTPS, rate limiting, logging, and professional review. Never reuse tutorial passwords in public systems. Remove sample accounts and disable verbose errors on live hosts.
Legal: Globaltuts.com is not responsible for security vulnerabilities if practice code is deployed on a live production server without proper sanitization. Read our practice project policy, Privacy Policy, and Disclaimer.