Right now I have a site that I only allow about five people to log into.
Those five people every time they login have to read an NDA and type their name in two text boxes provided. First and last name. When the user presses the submit button they are either taken to the site, if their name is correct, or logged out of the site if they type in anything other than their name.
Right now I am using javascript to do this since I don’t know how to use php to do this.
If the user does not enter his name in correctly the first time I want the user to be logged out, like what I have now, and I also want this users username to be altered by adding, “xxx10017969” to the end of his username. This will prevent him from logging back in and require him to contact me so I can fix his username in the database.
here is the HTML:
<form method="post" action="index.php" style="width: 600px;"><p>By typing your first and last name below you are bound by this contract. If you type anything other than your first and last name, you will be logged out and your account will be deleted.</p>
<p><input type="text" value="First Name" name="first_name" id="first_name" />
<input type="text" value="Last Name" name="family_name" id="last_name" />
<input type="date" value="Date" name="signiature" /></p>
<p><input type="button" value="Access Site" onclick="checkforname();" /><p>
</form>
And here is my javascript:
<script type="text/javascript">
function checkforname() {
var fname = "<?php echo "$identity->first_name"; ?>";
var lname = "<?php echo "$identity->family_name"; ?>";
var enteredFname = document.getElementById("first_name").value;
var enteredLname = document.getElementById("last_name").value;
if (enteredFname == fname && enteredLname == lname) {
}
else {
window.location = ("login.php?logout");
}
}
</script>
To do this in PHP instead of javascript –
The form submits to index.php, so that is where the logic should be.
If you want to leverage the way you redirect to login.php?logout, than index.php would have to have code something like this in it:
Keep in mind any headers should be processed before the php prints anything including newlines … so if index.php is an “in-line php” html page, make sure this happens at the very top of the file before the html portion starts. The opening
<?phptag should be on line 1.Also, this assumes you have a way to lookup the user again from a session or something to get the $identity variable populated with the user record.
There are some other things that ideally would be done here like sanitizing the user input in the post fields, and checking if post fields are empty to not generate warnings, but those are lessons for another time.
EDIT: Please show your existing php code when it is interacting with the database, like how it populates $identity, otherwise a specific solution cannot be provided.