Simple PHP Plaintext Login

<?php // define your password $password = 'your-password'; session_start(); ?> <?php if (isset($_POST['password'])) { //take the password from GET $user_password = $_POST['password']; //sanitize user input //(better sanitization can be achieved by using http://htmlpurifier.org/ ) $user_password = strip_tags($user_password); // Delete next line if the password should be case sensitive $user_password = strtolower($user_password); //check if password is correct if ($user_password == $password) { $_SESSION['logged_in'] = 'ok'; $message = 'You have successfully logged in.'; } else { $message = 'Wrong password!'; } } if ($_SESSION['logged_in'] == 'ok') { ?> <!-- protected content --> <p>Your Content</p> <!-- end protected content --> <!-- login form--> <?php } else { ?> <p><?php echo $message ?></p> <form action='index.php' method='post'> <input type='text' name='password' /> </form> <!-- end login form--> <?php // Delete next line to keep people logged in session_destroy(); ?>
If you want a very quick and easy solution to protect content within a PHP page you might want to use that snippet. To logic is simple. If the user types in the right password he will see the content. Otherwise he will see the login.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.