How can you make an own function in PHP such that I can bind to it different files according to the data in $_GET
?
I use the handler handler_login.php which only shows the login form if you are not logged in.
However, I need to run specific operations if the user is logged_in
or not_logged_in
. My current hnadler however is not so flexible to achieve this.
My handler_login.php
<?php
// Check for the existing Cookie
if (isset($_COOKIE['login']) ) {
//1. read the first word in Cookie of the form
//"[email protected],ca05106e445c15197f7213bc12648524
//Then, store this word to $email
$cookie_tripped = explode(",", $_COOKIE['login']);
$email = $cookie_tripped[0];
$result = pg_prepare($dbconn, "query1", 'SELECT passhash_md5 FROM users
WHERE email = $1;');
$result = pg_execute($dbconn, "query1", array($email));
if(!$result) {
exit;
}
// to take the passhash out of the cookie
$passhash_md5_cookie = $cookie_tripped[1];
if($result == $passhash_md5_cookie) {
header("Location: ../index.php");
die("logged in");
}
// put the login form visible // Problem HERE: I want that my function changes this value in the parameter
include 'forms/login.php';
}
?>
The problem in the above code is that it is not extensible.
The problem suggests me that I need two functions: not_logged_in(include_file)
and logged_in(include_file)
. The former should include the file in the parameter if the user not logged in. Otherwise, it should return nothing nothing new to the display.
The latter should then again include the file in the parameter if the user is logged in. Otherwise, it should do nothing.
My attempt in Pseudo code.
*Not_logged_in(paramer) -function*
// process all variables in the URL such as ?questions=777
not_logged_in(forms/login.php)
// this should return
// include 'forms/login.php'; if the user is not logged in
*Logged_in(paramer) -function*
// process all variables in the URL such as ?questions=777
logged_in(handler/send_question.php)
// this should return
// include 'handlers/send_question.php'; if the user is logged in