Name - Mihir Humar REG NO. - 18BCE0828 Internet and Web Programming Lab Exercise 9
Name - Mihir Humar REG NO. - 18BCE0828 Internet and Web Programming Lab Exercise 9
LAB EXERCISE 9
You can choose to do the above two steps directly in the MySQL Console or using PHP code.
3) Write a PHP file that can be added to other PHP files using the include or require functions. This file should:
• Make a connection to a MySQL database, using proper server name, username, password and db ame.
The connection resource should be stored in a variable with an appropriate name.
4) Sign-up
For this part, the entire code should be done in the same PHP file. The script should respond differently depending on
the situation (whether a POST request exists, whether the username is already taken, etc.). Wherever database connection
is required, use the script created in exercise 8. Do not add the db connection details to this page.
Write a PHP file that will output a form containing 3 fields: username, password, and phone number. These fields should
be sent via POST to the same file, which should take care of inserting them into the users table and then confirm the
registration by displaying the username and phone number back to the browser.
If the username already exists, your INSERT query should fail if you designated the USERNAME field as unique. You
should query the database before attempting the insert, and if the username exists already, display an error message and
a blank registration form again.
Note that the PASSWORD field assumes that you are storing a hex-string representation of a SHA-1/md5 hash of the
password. As explained in the lectures, you should never store passwords in plaintext. There are more secure ways of
storing the password. If you choose to use a different method, the PASSWORD_HASH field of the table may no longer be
CHAR(40) and you should
change it as appropriate.
5) Log-in
Write a PHP file that will output a form containing 2 fields: username and password. Upon submission of the form, the
code should check against the database to see whether the username-password pair was correct. If so, display a welcome
message. If not, display the message “Invalid username or password” followed by the same login form.
Once again, there should only be one PHP file, and you should redirect to the same place after submitting.
<?php
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$username= $_POST["username"];
$password= $_POST["password"];
$number= $_POST["number"];
if (isset($_POST['registerSubmit'])){
if( $username!="" && $password!="" && $number!=""){
$sql = "INSERT INTO user (password,number,username)
VALUES ('$password', '$number', '$username')";
if (mysqli_query($conn, $sql)) {
echo " WELCOME ! YOU HAVE SUCCESSFULLY SIGNED IN
";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
} else {
Echo "enter all the details";
}
}
mysqli_close($conn);
?>
<html>
<head><title></title></head>
<body>
<form method='POST'>
<label>Username</label><input type="text" name="username"></br>
<label>Password</label><input type="password" name="password"></br>
<label>Number</label><input type="number" name="name"></br>
<input type="submit" name="registerSubmit"></form>
<a href="login.php"> LogIn</a>
</body>
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
</html>
2.) LOGIN.PHP
<?php
error_reporting(0);
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$username= $_POST["username"];
$password= $_POST["password"];
if (isset($_POST['loginSubmit'])){
if( $username!="" && $password!="" ){
$sql = "SELECT id, username, password FROM user ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if($row["password"]==$password){
Echo "success";
$_SESSION["user"] = $row["username"];
$_SESSION["uid"] = $row["id"];
header("Location: home.php");
}
else{
Echo "Incorrect email or password";
}
}
} else {
echo "0 results";
}
}
}
mysqli_close($conn);
?>
<html>
<head><title></title></head>
<body>
<form method='POST'>
<label>Username</label><input type="text" name="email"></br>
<label>Password</label><input type="password" name="password"></br>
<input type="submit" name="loginSubmit"></form>
<a href="index.php">SignUp</a>
</body>
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
</html>
SCREENSHOTS-
SIGNUP PAGE -
LOGIN PAGE -