Reputation: 51
I couldn't find an answer to this question elsewhere, I was hoping someone here might know. I've spent 9 hours straight trying to get this to work, I've been searching non-stop.
I have three PHP files. The third file contains my PHP functions to check for existing users and add users to the database
user.inc.php
<?php
//checks if given username exists in database
function user_exists($user){
$user = mysqli_real_escape_string($user);
$sql= "SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'";
$result = mysqli_query($con,$sql);
if ($result == 1){
return true;
}
else {
return false;
}
}
function email_exists($email){
$user = mysqli_real_escape_string($email);
$sql="SELECT COUNT(`user_id`) FROM `users` WHERE `user_email` = '{$email}'";
$result = mysqli_query($con,$sql);
// $row = mysqli_fetch_assoc($total);
if ($result == 1){
return true;
}
else {
return false;
}
}
//checks if given username/password is valid
function valid_credentials($user,$pass){
$user = mysqli_real_escape_string(htmlentities($user));
$pass = sha1($pass);
$sql= "SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'AND `user_password` ='{$pass}'";
$result = mysqli_query($con,$sql);
if ($result == 1){
return true;
}
else {
return false;
}
}
//adds a user to the database
function add_user($user,$pass,$email){
mysqli_query($con,"INSERT INTO`users` (`user_name`,`user_password`,`user_email`) VALUES ('a','b','c')");
$user = mysqli_real_escape_string(htmlentities($user));
$pass = sha1($_REQUEST[$pass]);
mysqli_query($con,"INSERT INTO`users` (`user_name`,`user_password`,`user_email`) VALUES ('{$user}','{$pass}','{email}')");
}
?>
The second PHP file starts the connection to the database and includes the previous file at the bottom.
init.inc.php
<?php
session_start();
$exceptions = array('registerPage','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();
}
}
$con = mysqli_connect('127.0.0.1','root','','yingyujiaocheng');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO `users` (`user_name`,`user_password`) VALUES ('user','pass')");
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");
?>
The third PHP file includes the second file at the top and also has HTML and forms. When the form on the 3rd page is filled out and submitted, it sends the information to the top of the 3rd page to the PHP script. This is processed for errors which calls functions from the first files. The database works, I have done MYSQLI commands from every place, the only thing not working is the call from the 3rd PHP file to the first PHP file. The functions user_exists, email_exists and add_user to not call correctly.
Here is the third file:
registerPage.php
<?php error_reporting(E_ALL);
include('core/init.inc.php');
$errors = array();
if(isset($_POST['username'],$_POST['password'],$_POST['email'])){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if (empty($username)){
$errors[] = 'The username cannot be empty.';
}
if (empty($password)){
$errors[] = 'The password cannot be empty.';
}
if (empty($email)){
$errors[] = 'The email field cannot be empty.';
}
if (user_exists($username)){
$errors[] = 'The username is already taken';
}
if (email_exists($email)){
$errors[] = 'The email already taken';
}
if (empty($errors)){
add_user($username,$password,$email);
//$_SESSION['username'] = htmlentities($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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="ext/Styles/styleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="header" align="centre">
<img src="ext/Images/Logo.png" width="150" height="80" style="float:left;" />
<h2 class="headerFontClickedSmall" style="float: right; margin-top:36px; margin-right: 60px"> </h2>
<h2 class="headerFontUnclickedSmall" style="float: right; margin-top:36px; margin-right: 10px"> / </h2>
<h2 class="headerFontUnclickedSmall" style="float: right; margin-top:36px; margin-right: 10px"> </h2>
<h2 class="cornerBox1" style="float: right; margin-top:20px; margin-right: 50px"> </h2>
</div>
<div class="content">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<?php echo "Username is :" . $username . "<br>";
echo "Password is :" . $password;
?>
<br />
<h1 class="contentHeader" style=""> </h1>
<div>
<?php
if (empty($errors) === false){
?>
<ul>
<?php
foreach ($errors as $error){
echo "<li>{$error}</li>";
}
?>
</ul>
<?php
}
?>
</div>
<form action="registerPage.php" method="POST">
<h1 class="contentRegisterText" style=""> : <input class="inputbox" style="margin-left:30px" type="text"
name = "username" id="username"/> </h1>
<br />
<h1 class="contentRegisterText" style=""> : <input class="inputbox" style="margin-left:30px" type="text"
name = "email" id="email"/> </h1>
<br />
<h1 class="contentRegisterText" style="">: <input class="inputbox" style="margin-left:30px" type="password"
name = "password" id="password"/> </h1>
<br /><br /><br /><br /><br /><br />
<input style="margin-left:30px" type="submit" value = "" id="Register"/> </form>
</div>
<div class="footer" align="center" >
<div class="floating-box" style="margin-top:40px" >
<dl>
<dt><h1 class="footerTitle">社交媒体</h1></dt>
<br />
<dd><a href="http://www.huya.com/lucio">
<img src="ext/Images/HuyaLogo.png" alt=" " width="42" height="42" outline="none">
</a>
<img src="ext/Images/weixinLogo.png" width="40" height="40" style="margin-left:3;"/><img src="ext/Images/logo-qq.png" width="40" height="40" /></dd>
</dl>
</div>
<div class="floatingboxFooter1" style="margin-top:40px">
<dl>
<dt><h1 class="footerTitle"></h1> </dt>
<br />
<dd><h1 class="footerSmall">:</h1></dd>
<dd><h1 class="footerSmall">Weixin: </h1></dd>
<dd><h1 class="footerSmall">QQ: </h1></dd>
</dl>
</div>
<div class="floating-box" style="margin-top:64px">
<dl>
<br />
<dd><h1 class="footerSmall">[email protected]</h1></dd>
<dd><h1 class="footerSmall">yingyujiaocheng</h1></dd>
<dd><h1 class="footerSmall">yingyujiaocheng</h1></dd>
</dl>
</div>
<div class="floatingboxFooter1" style="margin-top:40px">
<dl>
<dt><h1 class="footerTitle"></h1> </dt>
<br />
<dd><h1 class="footerSmall"></h1></dd>
<dd><h1 class="footerSmall"></h1></dd>
<dd><h1 class="footerSmall"></h1></dd>
</dl>
</div>
</body>
</html>
Thank you for looking, any help is appreciated,
Cheers
Lucio
Upvotes: 1
Views: 132
Reputation: 100
Try to debug the $path
variable, I think the problem is in the path you included in this line.
include("{$path}/inc/user.inc.php");
Or try to insert global $con
on top of every function inside user.inc.php like so:
function user_exists($user) {
global $con;
// rest of the code...
}
Upvotes: 2