I need to make a contact page that only allows a person to email if they are logged in and if they're logged in to automatically show the email form. If not logged in to either redirect them to the login page, or have them login right on the contact page.
What's the best(easiest) way to do that?
I'm having trouble also with looking up logins that I have stored in my users directory. Can anyone find the problem with this code? It keeps saying username and password aren't found, when I know they're there and can see them when I look up my accounts.txt file.
<?php // Script 11.7 - login.php
// This script displays and handles an HTML form.
// This script logs a user in by check the stored values in text file.
// Address error handing.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
session_name('YourVisit');
session_start();
if (isset ($_POST['submit'])) { // Handle the form.
$loggedin = FALSE; // Not currently logged in.
// Open the file.
$fp = fopen ('users/accounts.txt', 'rb');
// Loop through the file.
while ( $line = fgetcsv ($fp, 100, "\t")) {
// Check the file data against the submitted data.
if ( ($line[0] == $_POST['user_name']) AND ($line[1] == crypt ($_POST['password'], $line[1]) ) ) {
$loggedin = TRUE; // Correct username/password combination.
// Stop looping through the file.
break;
} // End of IF.
} // End of WHILE.
fclose ($fp); // Close the file.
// Print a message.
if ($loggedin) {
print '<p>You are now logged in.</p>';
} else {
print '<p>The username and password you entered do not match those on file.</p>';
}
} else { // Display the form.
// Leave PHP and display the form.
?>
<form action="index.php" method="post">
Username: <input type="text" name="username" size="20" /><br />
Password: <input type="password" name="password" size="20" /><br />
<input type="submit" name="submit" value="Login" />
</form>
<?php
}// End of SUBMIT IF.
?>
Thank you