Login functionality of a client's site isn't working with the visual update I'm giving it. A large part of the site is also running on outdated php so I'm updating it.
I'm currently working on the Q&A portion of the site.
index.php force redirects to login.php if user isn't logged in; login.php references a config file called define.mysqli.php that establishes a connection to the db, while also requiring a func.php file has contains a numerous amount of functions such as getQuestions() and getUserInfoById(); connection is fine, but the functionality of the site itself isn't working due to errors such as mysqli_real_escape_string(): Couldn't fetch mysqli in login.php (error 1), there are undefined variables in the func.php file (error 2), mysqli_query() expects parameter 1 to be mysqli, null given in func.php (error 3)
I just need a hand pinpointing what else I need to change. The other questions on StackOverflow have been too simple; I understand what changes I need to make but there's too much that I'm working with and I work as a jr dev w/out a sr dev as a mentor.
Error 1: mysqli_real_escape_string(): Couldn't fetch mysqli in login.php
//top of code
<?php
//Display errors; COMMENT OUT WHEN NOT DEBUGGING
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//DB
require('lib/func.php');
//The function looks like this
//Checks if user is already logged in and redirects.
if (isset($_SESSION['user'])){
$user = getUserInfo($_SESSION['user']);
if ($user != null) {
if ($user['admin'] == 1) {
header('Location: admin.php');
exit;
} else {
header('Location: login.php');
}
}
}
if (isset($_POST)){
if(sizeof($_POST) > 0) {
$user = mysqli_real_escape_string($link, trim($_POST['username'])); //error
$pass = mysqli_real_escape_string($link, trim($_POST['password'])); //error
if(isValidAdminUser($user,$pass)) {
persistUser($user);
header('Location: admin.php');
exit;
} elseif(isValidStateUser($user,$pass)) {
persistUser($user);
header('Location: /');
exit;
} else {
$error = "Invalid username or password";
}
}
}
//what am i missing from this? what's am i doing wrong/needs improvement?
Error 2: undefined variables in func.php
//top of file
<?php
include("define.mysqli.php");
if(!isset($_SESSION)){
session_start();
}
//....
function isValidAdminUser($uname,$pass) {
$query = "SELECT * FROM users WHERE login = '$uname' AND admin = 1";
$result = mysqli_query($link, $query) or die(mysqli_error($link)); //error
if(mysqli_num_rows($result) > 0) {
$user = mysqli_fetch_array($result); //error
if(md5($pass) == $user['password'])
return true;
}
return false;
}
//the original code was
function isValidAdminUser($uname,$pass) {
$query = "SELECT * FROM users WHERE login = '$uname' AND admin = 1";
$result = mysql_query($query) or die(mysql_error()); //error
if(mysqli_num_rows($result) > 0) {
$user = mysqli_fetch_array($result); //error
if(md5($pass) == $user['password']) {
return true;
}
}
return false;
}
//i changed it to mysqli best i could but $link is undefined???
Error 3: mysqli_query() expects parameter 1 to be mysqli, null given in func.php
//in same code as above
this is what my config file (define.mysqli.php) looks like
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
if(!isset($_SESSION)){
session_start();
}
if(!defined("DATABASE_PREFIX"))
define("DATABASE_PREFIX","dbr_");
$hostname = "127.0.0.1";
$username = "*correct_username";
$password = "*correct_password";
$database = "*correct_database";
$link = mysqli_connect($hostname, $username, $password, $database);
if(!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
//echo "Success: Established MySQL connection." . PHP_EOL;
//echo "Host Information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
I'm trying my best to change the parameters from mysql_* to mysqli_* correctly but I don't know what I'm missing.