http://php.net/manua...e.functions.php
http://www.php.net/m...s.arguments.php
but it doesn't attempt to explain why you should use them.
Imagine if you could break your project into several smaller pieces that you could simply describe to the computer in something close to plain English. Wouldn't that make things easier? It turns out that you can, or at least very close to it. Look at this code:
<?php
$username = '';
$loggedin = retrieve_logged_in_session_status($username);
if($loggedin) {
show_welcome_page($username);
} elseif(validate_user_input() && check_user_password()) {
show_welcome_page($username);
} else {
show_login_page();
}
?>
It's pretty straight forward, isn't it? First, I'm telling the computer that I want to see if the user has already logged in. If they have already logged in then I want to show them the welcome page. If not, I check to see if they've entered valid input for logging in. If they have I check to see if they've entered a valid user name and password, and if they have I show them the welcome page. Finally, if they are not logged in and they haven't given a valid user name and password I show them the login page.
At this point you might be thinking that this is too easy. Well, there is a bit more! The code above will not run as it is. It needs that plain english defined for the computer so that it knows what to do. Below I've defined a couple of the functions. You can use the links provided above to see how the functions work, then you might try writing the other two functions and getting this code to work!
function retrieve_logged_in_session_status(&$user) {
session_start();
if(isset($_SESSION['Username'])) {
$user = $_SESSION['Username'];
return true;
}
return false;
}
function show_welcome_page($user) {
echo str_replace("USERNAME", $user, file_get_contents("welcome.html"));
}






MultiQuote



|