PHP Tutorial: Register and Login (Cookie Extension) [part 01
Posted: Wed Sep 05, 2012 12:44 am
I typed what the tutorial said, and it says my password and username is incorrect. Here is the code I typed:
init.inc.php file:
init.inc.php file:
<?php //init.inc.php file session_start(); $exceptions = array('signup', 'login', 'index'); $page = substr(end(explode( '/', $_SERVER['SCRIPT_NAME'])), 0, -4); // SQL stuff. mysql_connect('SQL host','username','password'); mysql_select_db('database name'); include('user.inc.php'); //$_SESSION['uid'] = 1; if (isset($_COOKIE['username'], $_COOKIE['password'])){ if (valid_credentials($_COOKIE['username'], $_COOKIE['password'])){ $_SESSION['username'] = htmlentities($_COOKIE['username']); setcookie('username', $_COOKIE['username'], time() + 3600); setcookie('password', $_COOKIE['password'], time() + 3600); } } if (in_array($page, $exceptions) === false){ if (isset($_SESSION['username']) === false){ header( 'Location: login.php' ); die(); } } ?>Login.php
<?php include('init.inc.php'); $errors = array(); if (isset($_POST['username'], $_POST['password'])){ if (empty($_POST['username'])){ $errors[] = 'The username field is empty.'; } if (empty($_POST['password'])){ $errors[] = 'The password field is empty.'; } if (valid_credentials($_POST['username'], sha1($_POST['password'])) === false){ $errors[] = 'Username or password is incorrect.'; } if (empty($errors)){ setcookie('username', $_POST['username'], time() + 3600); setcookie('password', sha1($_POST['password']), time() + 3600); $username = $_POST['username']; session_register("username"); header( 'Location: home.php' ); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <body> <form action="" method="post"> <p> <?php if (empty($errors) === false){ ?> <ul> <?php foreach ($errors as $error){ echo "<center><li>[$error]</li></center>"; } ?> </ul> <? } ?> </p> <input type="text" name="username" id="username" placeholder="Username" maxlength="40" value="<?php if (isset($_POST['username'])){ echo htmlentities($_POST['username']); } ?>"><br><br> <input type="password" name="password" placeholder="Password"><br><br> <input type="submit" value="Login"> </form> </body> </html>The problem is that it won't let me login. It's kinda annoying since I was looking for hours on the internet.