I am learning Php at Coursera and right now we are supposed to do a simple CRUD (create, read, update, delete) web application.
For my login program I "outsourced" all error messages to a separate file called "errors.php":
<?php
$errors = array(
"email" => "Email must have an at-sign (@)",
"login" => "Email and password are required",
"pass" => "Incorrect password");
?>
And now I wanted to include (request) it within my login routine. As you see I included/requested it twice. From what I understand the first request should be enough for the errors-variable to be available within the whole login script. It should have a global scope. Correct?
But after quite some debugging I found out that I have to request it within the function "returnOnerror" to be available within this function call.
Aren't global variables accessible within a function? The other way round, I would understand that...
<?php
session_start();
require("errors.php");
function returnOnerror($whaterror){
require("errors.php");
$_SESSION["error"] = $errors[$whaterror];
header('location: login.php');
exit();
}
if ((isset($_POST['email'])) && (isset($_POST['pass']))){
$salt = 'XyZzy12*_';
$stored_hash = '1a52e17fa899cf40fb04cfc42e6352f1'; //(salted hash + php123)
$passhash = hash('md5', $salt.$_POST['pass']);
if ((strlen($_POST['email']<1)) && (strlen($_POST['pass'])<1)) {
returnOnerror("login");
}
if ((substr_count($_POST['email'], '@'))<1){
returnOnerror('email');
}
if ($passhash==$stored_hash){
$_SESSION['name'] = $_POST['email'];
$_SESSION['login'] = "logged in";
header('location: index.php');
exit();
}
else {
returnOnerror("pass");
}
}
?>
Btw, I searched here on Stackexchange and on the web to find an explanation but wasn't able. Thus I turned over here as the last chance. Thanks.