When I put the user_exists function in the register.php file and make the error , it doesnt tell me user already exists or when i use the add_user function its not working like my php is not connecting to my database.
user.inc.php
<?php
// checks if given username exists
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;
}
//checks if given username and combination is valid
function valid_combination($user,$pass){
$user=mysql_real_escape_string($user);
$pass=md5($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 user to database
function add_user ($user,$pass){
$user=mysql_real_escape_string(htmlentities($user));
$pass=md5($pass);
mysql_query("INSERT INTO 'users' ('user_name' , 'user_password') VALUES ( '{$user}', '{$pass}')" );
}
?>
register.php
<?php
include('init.inc.php');
$errors=array();
if (isset($_POST['username'], $_POST['password'], $_POST['repeate_password'])){
if(empty($_POST['user_name']))
$errors[]='username cannot be empty </br>';
if(empty($_POST['password']) || empty($_POST['repeate_password'])){
$errors[]='password cannot be empty';
}
}
if($_POST['password'] !== $_POST['repeate_password']){
$errors[]='passwords do not match';
}
if(user_exists($_POST['username'])){
$errors[]='username already exists';
if(empty($errors)){
add_user($_POST['username'], $_POST['password']);
$_SESSION['username']=htmlentities($_POST['username']);
header('Location: protected.php');
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="alihamie" />
<title>Untitled 6</title>
</head>
<body>
<p>
<div>
<?php
if (empty($errors)=== false){
}
?>
<ul>
<?php
foreach($errors as $error){
echo "<li>{$error}</li>";
}
?>
</ul>
<?php
?>
</div>
</p>
<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="repeate_password">Repeate password:</label>
<input type="password" name="repeate_password" id="repeate password" />
</p>
<p>
<input type="submit" value="Register"/>
</p>
</body>
</html>
init.inc.php
<?php
session_start();
$exceptions=array('register','login');
$page=substr(end(explode('/',$_SERVER['SCRIPT_NAME'])),0,-4);
if(in_array($page,$exceptions)=== false){
if(isset($_SESSION['username'])=== false){
header('location:login.php');
die();
}
}
mysql_connect('$server','$usernamee','$password');
mysql_select_db('user_system');
include('user.inc.php');
?>