undifined variable $con.
Ik ben bezig met een login systeempje alleen had ik eerst alles in mysql omdat dat in de tutorial zo werd gedaan, nu wil ik dit over zetten naar mysqli alleen nu krijg ik problemen met de variable $con.
Hopelijk kunnen jullie me helpen.
init.php
Code (php)
connect.php
Code (php)
1
2
3
4
2
3
4
<?php
$error_message = "There are some connection isseus this is not a problem by u. We are currently bussy, so please come back later.";
$con = mysqli_connect('localhost', 'root', '123', 'mtut') or die ($error_message);
?>
$error_message = "There are some connection isseus this is not a problem by u. We are currently bussy, so please come back later.";
$con = mysqli_connect('localhost', 'root', '123', 'mtut') or die ($error_message);
?>
users.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
function user_exists($username) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username'");
return (mysqli_fetch_array($query, 0) == 1) ? true : false;
}
function user_active($username) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username' AND active = 1");
return (mysqli_fetch_array($query, 0) == 1) ? true : false;
}
function user_id_from_username($username) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username'");
return mysqli_fetch_array($query, 0, 'user_id');
}
function login($username, $password) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
//$password = password_hash($password, PASSWORD_BCRYPT);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username' AND password = '$password'");
return (mysqli_fetch_array($query, 0) == 1) ? $user_id : false;
}
?>
function user_exists($username) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username'");
return (mysqli_fetch_array($query, 0) == 1) ? true : false;
}
function user_active($username) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username' AND active = 1");
return (mysqli_fetch_array($query, 0) == 1) ? true : false;
}
function user_id_from_username($username) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username'");
return mysqli_fetch_array($query, 0, 'user_id');
}
function login($username, $password) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
//$password = password_hash($password, PASSWORD_BCRYPT);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username' AND password = '$password'");
return (mysqli_fetch_array($query, 0) == 1) ? $user_id : false;
}
?>
general.php
Enne verder...
MySQLi gebruiken, met de verouderde mysql_real_escape_string() functie?
En oja miss zou het handig zijn als ik dit mee geef.
ERROR[
Warning: Missing argument 2 for user_exists(), called in C:\xampp\htdocs\ProjectTut\login.php on line 12 and defined in C:\xampp\htdocs\ProjectTut\core\functions\users.php on line 2
Notice: Undefined variable: con in C:\xampp\htdocs\ProjectTut\core\functions\users.php on line 5
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\ProjectTut\core\functions\users.php on line 5
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\ProjectTut\core\functions\users.php on line 6
Array ( [0] => This username doesn't exist. Please register. )
]
Als je $con als parameter gebruikt, moet je de aanroep van de functie ook natuurlijk aanpassen. Tenzij het een optionele parameter is, maar dat lijkt me niet handig hiervoor.
Global zou eventueel ook wel kunnen, dan hoef je de parameters niet aan te passen.
Gewijzigd op 20/06/2014 21:48:33 door - Ariën -
Toevoeging op 20/06/2014 21:56:01:
Even de update van wat er veranderd is.
users.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
function user_exists($username, $con) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username'");
return (mysqli_fetch_array($query, 0) == 1) ? true : false;
}
function user_active($username, $con) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username' AND active = 1");
return (mysqli_fetch_array($query, 0) == 1) ? true : false;
}
function user_id_from_username($username, $con) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username'");
return mysqli_fetch_array($query, 0, 'user_id');
}
function login($username, $password, $con) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
//$password = password_hash($password, PASSWORD_BCRYPT);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username' AND password = '$password'");
return (mysqli_fetch_array($query, 0) == 1) ? $user_id : false;
}
?>
function user_exists($username, $con) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username'");
return (mysqli_fetch_array($query, 0) == 1) ? true : false;
}
function user_active($username, $con) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username' AND active = 1");
return (mysqli_fetch_array($query, 0) == 1) ? true : false;
}
function user_id_from_username($username, $con) {
$username = sanitize($username);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username'");
return mysqli_fetch_array($query, 0, 'user_id');
}
function login($username, $password, $con) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
//$password = password_hash($password, PASSWORD_BCRYPT);
$query = mysqli_query($con, "SELECT user_id FROM users WHERE username = '$username' AND password = '$password'");
return (mysqli_fetch_array($query, 0) == 1) ? $user_id : false;
}
?>
login.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
include 'core/init.php';
if (empty($_POST) == false) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username)) {
$errors[] = "Please fill in a username.";
} else if (empty($password)) {
$errors[] = "Please fill in a password.";
} else if (user_exists($username, $con) == false) {
$errors[] = "This username doesn't exist. Please register.";
} else if (user_active($username, $con) == false) {
$errors[] = "You haven't activated your account.";
} else {
$login = login($username, $password, $con);
if ($login == false) {
$errors[] = "This password doesn't match with this username.";
} else {
}
}
print_r($errors);
}
?>
include 'core/init.php';
if (empty($_POST) == false) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username)) {
$errors[] = "Please fill in a username.";
} else if (empty($password)) {
$errors[] = "Please fill in a password.";
} else if (user_exists($username, $con) == false) {
$errors[] = "This username doesn't exist. Please register.";
} else if (user_active($username, $con) == false) {
$errors[] = "You haven't activated your account.";
} else {
$login = login($username, $password, $con);
if ($login == false) {
$errors[] = "This password doesn't match with this username.";
} else {
}
}
print_r($errors);
}
?>
general.php
Code (php)
1
2
3
4
5
2
3
4
5
<?php
function sanitize($data, $con) {
return mysqli_real_escape_string($con, $data);
}
?>
function sanitize($data, $con) {
return mysqli_real_escape_string($con, $data);
}
?>
Je moet de aanroep naar sanitize() ook veranderen. Nu krijg je waarschijnlijk Missing argument 2 for sanitize()
Aar klopt.
return (mysqli_fetch_array($query, 0) == 1) ? true : false;