Solution:
I tinkered around with kcdwayne's test (using test.php and register.php) and determined that the issue resided in using the file name "db.php." I renamed it to "datab.php" and it appears to be working wherever it is being used. Interesting. Thank you for your answers!
Original Post:
I have one file - checkuser.php
- that is POST'd to from a login form to verify a user's credentials. username
and password
are given. Through an external file - db.php
- I am trying to establish a connection to the MySQL database. The setup:
checkuser.php:
session_start();
error_reporting(E_ALL);
ini_set( 'display_errors','1');
require "db.php";
db.php:
$con = mysqli_connect("server", "user", "pass", "db");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
... later in checkuser.php:
$username = $_POST['username'];
$password = $_POST['password'];
$sql = mysqli_query($con, "SELECT * FROM userdata WHERE username='$username' AND password='$password' AND activation='true'");
And thus, here lies my problem:
Notice: Undefined variable: con in /path/to/checkuser.php on line 23
checkuser.php
and db.php
are in the same folder; the MySQL connection can easily be established if the code in db.php
is moved into checkuser.php
itself.
What am I doing wrong?
checkuser.php:
<?php
session_start();
error_reporting(E_ALL);
ini_set( 'display_errors','1');
require "db.php";
$username = $_POST['username'];
$password = $_POST['password'];
if((!$username) || (!$password)){
echo "<font color='white'>Please enter ALL of the information! <br />";
include 'login.php';
exit();
}
$password = md5($password);
$sql = mysqli_query($con, "SELECT * FROM userdata WHERE username='$username' AND password='$password' AND activation='true'");
$login_check = mysqli_num_rows($sql);
if($login_check > 0) {
while($row = mysqli_fetch_assoc($sql)) {
... set some $_SESSION variables ...
}
}
?>
db.php:
<?php
$con = mysqli_connect("server", "user", "password", "database");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>