Omar Patel
Software developer/instructor
Last week, we finished going over how to create a registration system and how to set and utilize cookies.
Here is the end result result of our register and login pages:
The following is a link that shows how the end result of the register.php page should look like:
https://goo.gl/Hpca91
The final result of our login.php should look like:
https://goo.gl/BjCepU
Building CMS
Updating Information
Creating the Form
Creating the Form
<h1>Update Information</h1>
<form method="post" action="">
<ul>
<li>
<label for="old_username">Enter Existing Username</label>
<input id="old_username" type="text" name="old_username" value="" />
</li>
<li>
<label for="old_password">Enter Existing Password</label>
<input id="old_password" type="text" name="old_password" value="" />
</li>
<li>
<label for="new_username">Enter New Username</label>
<input id="new_username" type="text" name="new_username" value="" />
</li>
<li>
<label for="new_password">Enter New Password</label>
<input id="new_password" type="password" name="new_password" value=""/>
<li>
<li>
<label for="new_name">Enter New Name</label>
<input id="new_name" type="text" name="new_name" value=""/>
<li>
<input type="submit" name="update" value="update">
</li>
</ul>
</form>Checking username and password
Checking username and password
// Check to see if user has posted the information
if (isset($_POST['update']) && trim($_POST['update']) != '') {
if (isset($_POST['old_username']) && isset($_POST['old_password'])
&& trim($_POST['old_username']) != '' && trim($_POST['old_password']) != '') {
// Save and check old username and password
$old_username = escape_quotes($_POST['old_username']);
$old_password = escape_quotes(hash("sha512", $_POST['old_password']));
$user = get_all_info("SELECT * FROM users WHERE Username='$old_username'");
// Get the first instance of the user and store it into an array
$userArray = $user->fetch_assoc();
if(count($userArray) <= 0) {
die("That username doesn't exist! Please type in the correct username.
<a href='update.php'>Back</a>");
}
if ($userArray['Password'] != $old_password) {
die("Incorrect password! <a href='update.php'>Back</a>");
}Checking name and updating name
if ($userArray['Password'] != $old_password) {
die("<h2>Incorrect password! <a href='update.php'>Back</a></h2>");
}
$new_name = '';
if ($_POST['new_name']) {
// Get the existing name if users input the name
$old_name = $userArray['Name'];
$new_name = escape_quotes(strip_tags($_POST['new_name']));
insert_or_update_info("UPDATE users SET Name='$new_name'
WHERE Name='$old_name'");
echo "<h2>Name has been updated. Please <a href='login.php'>log in</a>
with your new credentials. </h2><br>";
} else {
echo "<h2>Since no Name was given, Name is still " . $userArray['Name'] . "</h2><br>";
}Checking name and updating name
// Check new username if user put it
if (trim($_POST['new_username']) != '' && isset($_POST['new_username']) ) {
$new_username = escape_quotes(strip_tags($_POST['new_username']));
$check = get_all_info("SELECT * FROM users WHERE Username='$new_username'");
// Get the first instance of the user and store it into an array
$userArray = $check->fetch_assoc();
if (count($userArray) > 0) {
die("<h2>That username already exists! Try creating another username.
<a href='register.php'>Back</a></h2>");
}
if (!ctype_alnum($new_username)) {
die("<h2>Username contains special characters! Only numbers and letters
are permitted. <a href='update.php'>Back</a></h2>" );
}
if (strlen($new_username) > 20) {
die("<h2>Username must contain less than 20 characters.
<a href='update.php'>Back</a></h2>" );
}
insert_or_update_info("UPDATE users SET Username='$new_username'
WHERE Username='$old_username'");
echo "<h2>Username has been updated. Please <a href='login.php'>log in</a>
with your new credentials. </h2><br>";
} else {
echo "<h2>Since no Username was given, Username is still "
. $userArray['Username'] . "</h2><br>";
} // Check new password
if (trim($_POST['new_password']) != '' && isset($_POST['new_password'])) {
$new_password = escape_quotes(hash("sha512", $_POST['new_password']));
insert_or_update_info("UPDATE users SET Password='$new_password'
WHERE Password='$old_password'");
echo "<h2>Password has been updated. Please <a href='login.php'>log in</a>
with your new credentials. <h2><br>";
} else {
echo "<h2>Since no Password was given, Password remains the same. </h2><br>";
}
}
else {
echo "<h2>Please enter a username and password.</h2>";
}Here is the final result of our update.php file: https://goo.gl/ON4iQn
By Omar Patel