register.php
<?php include('core\init.inc.php'); $errors = array(); if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){ if (empty($_POST['username'])){ $errors[] = 'The username cannot be empty.'; } if(empty($_POST['password']) || empty($_POST['repeat_password'])){ $errors[] = 'The password cannot be empty.'; } if ($_POST['password'] !== $_POST['repeat_password']){ $errors[] = 'Password verification failed.'; } if (user_exists($_POST['username'])){ $errors[] = 'The username you entered is already taken.'; } if (empty($errors){ add_user($_POST['username'], $_POST['password']); $_SESSION['username'] = htmlentities($_POST['username']); header('Location: protected.php'); die(); } } ?> <html> <head> <title>The Video Network</title> </head> <body bgcolor="59D7F0"> <div> <?php if (empty($errors) === false){ ?> <ul> <?php foreach ($errors as $error){ echo "<li>{$error}</li>"; } ?> </ul> <?php } ?> </div> <table> <tr> <td><a href="homepage.php"><img src="ext\images\logo.png"></a></td> <td><font size="5" color="59D7F0"><B>fille</B></font> <td><img src="ext\images\topuploaders.png"></td> <td><img src="ext\images\mostloved.png"></td> <td><img src="ext\images\mostview.png"></td> <td><font size="5" color="59D7F0"><B>fille</B></font> <td><a href="login-register/login.php"><img src="ext\images\login.png"></a></td> </tr> </table> <br> <br> <br> <br> <br> <br> <center> <form action="" method="post"> <p> <label for="username"> Username:</label> <input type="text" name="username" id="username" /> </p> <p> <label for="password"> Password:</label> <input type="password" name="password" id="password" /> </p> <p> <label for="repeat_password">Repeat Password:</label> <input type="password" name="repeat_password" id="repeat_password" /> </p> <p> <input type="submit" value="Register" /> <p> </form> </center> </body> </html>protected.php
<?php include('core\init.inc.php'); ?> <html> <head> <title>Login|The Video Network</title> </head> <body> <p> You are logged in as <?php ?> </p> </body> </html>init.inc.php
<?php session_start(); $exceptions = array('register', 'login'); if (in_array($page, $exceptions) === false){ if (isset($_SESSION['username']) === false){ header('location: login.php'); die(); } } mysql_connect('10.0.0.1', 'example_pass', 'example_pass'); mysql_select_db('user_system'); $path = dirname(_FILE_); include("{$path}/inc/user.inc.php"); ?>user.inc.php
[syntax]<?php
// checks if the given username exists in the table
function user_exists($user){
$user = mysql_real_escape_string($user);
$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}'");
return (mysql_result($total, 0) == '1')) ? true : false;
}
// check if the given username and password combination is valid.
function valid_credentials($user, $pass){
$user = mysql_real_escape_string($user);
$pass = shal($pass);
$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}' AND 'user_password' = '{$pass}'");
return (mysql_result($total, 0) == '1')) ? true : false;
}
// adds a user to the database.
function add_user($user, $pass){
$user = mysql_real_escape_string(htmlentities($user));
$pass = shal($pass);
mysql_query("INSERT INTO 'users' ('user_name', 'user_password') VALUES ('{$user}', '{$pass}')");
}
?> [/syntax]