0% found this document useful (0 votes)
65 views

Exercise-8 IWP 18BCE0557 Kushal: 1) DB Creation Code

The document describes the process of creating a database and table for user registration and login functionality in PHP. It includes code to: 1. Connect to a MySQL database and create a "Users" database and "user" table with fields for username, password hash, and phone number. 2. Provide signup and login forms that insert user data into the "user" table and validate credentials. 3. The signup form hashes the password before insertion and checks for duplicate usernames. 4. The login form queries the database to validate the username and password and redirects to a message on incorrect credentials.

Uploaded by

Kush Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Exercise-8 IWP 18BCE0557 Kushal: 1) DB Creation Code

The document describes the process of creating a database and table for user registration and login functionality in PHP. It includes code to: 1. Connect to a MySQL database and create a "Users" database and "user" table with fields for username, password hash, and phone number. 2. Provide signup and login forms that insert user data into the "user" table and validate credentials. 3. The signup form hashes the password before insertion and checks for duplicate usernames. 4. The login form queries the database to validate the username and password and redirects to a message on incorrect credentials.

Uploaded by

Kush Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Exercise-8

IWP
18BCE0557 KUSHAL

1) DB creation
CODE:
<?php
header​(​'Content-type: text/plain'​);

$url​=​'127.0.0.1:3306'​;
$username​=​'root'​;
$password​=​''​;
$dbname​ = ​'Users'​;
$conn​=​mysqli_connect​(​$url​, ​$username​, ​$password​);

if​(​$conn​->​connect_error​){
​die​(​'Could not Connect My Sql:'​ . ​$conn​->​connect_error​);
}

// IF DB_ID('SqlHintsDB') IS NOT NULL


// BEGIN
// PRINT 'Database Exists'
// END

$sql​ = ​"DROP DATABASE Users"​;


if​ (​$conn​->​query​(​$sql​) === ​TRUE​) {
​echo​ ​"​\n​Database user dropped"​;
}
else
{
​echo​ ​"​\n​Error dropping database: "​ . ​$conn​->​error​;
}

/* Create database */
$sql​ = ​"​CREATE​ ​DATABASE​ Users"​;
if​ (​$conn​->​query​(​$sql​) === ​TRUE​) {
​echo​ ​"​\n​Database Users created successfully"​;
}
else
{
​echo​ ​"​\n​Error creating database: "​ . ​$conn​->​error​;
}

$conn​->​close​();

$conn​=​mysqli_connect​(​$url​, ​$username​, ​$password​, ​$dbname​);

if​(​$conn​->​connect_error​){
​die​(​'\n2 Could not Connect My Sql:'​ . ​$conn​->​connect_error​);
}

$sql​ = ​"​CREATE​ ​TABLE​ user(


ID ​int​ ​NOT​ ​NULL​ AUTO_INCREMENT,
USERNAME ​varchar​(​50​) UNIQUE,
PASSWORD_HASH ​char​(​32​),
PHONE ​VARCHAR​(​10​),
​primary​ ​key​ (id)
)"​;

if​ (​$conn​->​query​(​$sql​) === ​True​) {


​echo​ ​"​\n​Table created :)"​;
} ​else​ {
​echo​ ​"​\n​error in creating table"​;
​echo​ ​$conn​->​error​;
}

$conn​->​close​();

?>
2) CONNECTION Code
3) <?php
4) $url​=​'127.0.0.1:3306'​;
5) $username​=​'root'​;
6) $password​=​''​;
7) $database​=​'Users'​;
8) $conn​=​mysqli_connect​(​$url​,​$username​,​$password​,​$database​);
9) if​(!​$conn​){
10) ​die​(​'Could not Connect My Sql:'​ .​mysql_error​());
11) }
12) else​ {
13) ​echo​ ​"All good here"​;
14) }
15) ?>
3) Signup
<!​DOCTYPE​ ​html​>
<​html​ ​lang​=​"en"​>
<​head​>
​<​meta​ ​charset​=​"UTF-8"​>
​<​meta​ ​name​=​"viewport"​ ​content​=​"width=device-width, initial-scale=1.0"​>
​<​title​>​signup form​</​title​>

​<​script​ ​language​=​"javascript"​>
function​ ​check​()
{

​if​(​document​.​form1​.​username​.​value​==​""​)
{
​alert​(​"Plese Enter Username"​);
​document​.​form1​.​username​.​focus​();
​return​ ​false​;

​if​(​document​.​form1​.​password​.​value​==​""​)
{
​alert​(​"Plese Enter Your passwordword"​);
​document​.​form1​.​password​.​focus​();
​return​ ​false​;
}

​if​(​document​.​form1​.​pnumber​.​value​==​""​)
{
​alert​(​"Plese Enter Phone No"​);
​document​.​form1​.​pnumber​.​focus​();
​return​ ​false​;
}

​return​ ​true​;
}

</​script​>

</​head​>
<​body​>

​<?php
​if​ (​$_SERVER​[​'REQUEST_METHOD'​] === ​'GET'​) {
​echo​ ​'<div>
<form name="form1" action="signup.php" method="post"
onSubmit="return check();">
<input type="text" name="username" id="username"
placeholder="Username">
<input type="password" name="password" id="password"
placeholder="Password">
<input type="number" name="pnumber" id="pnumber"
placeholder="Phone Number">
<input type="submit" value="submit">
</form>
</div>'​;
}

​if​ (​$_SERVER​[​'REQUEST_METHOD'​] === ​'POST'​) {

​extract​(​$_POST​);
​include​(​"database.php"​);

​$rs​=​mysqli_query​(​$conn​,​"select * from user where


username='​$username​'"​);
​if​ (​mysqli_num_rows​(​$rs​)>​0​) {
​echo​ ​"Username ​$username​ already exists in the db"​;
​exit​;
}
​$password_hash​ = ​hash​(​'md5'​, ​$password​);
​$query​=​"insert into user (USERNAME,PASSWORD_HASH,PHONE)
values('​$username​','​$password_hash​','​$pnumber​')"​;
​//$query = "SELECT * FROM user";
​$rs​=​mysqli_query​(​$conn​,​$query​);
​echo​ ​mysqli_error​(​$conn​);
​echo​ "​ Username: ​$username​"​;
​echo​ "​ </br>"​;
​echo​ ​"Phone Number ​$pnumber​"​;
​$query2​ = ​"select * from user"​;
​$rs​ = ​mysqli_query​(​$conn​, ​$query2​);
​// while ($row = $rs->fetch_assoc()) {
​// echo $row."<br>";
​// }
}

​?>

</​body​>
</​html​>

4) Login
CODE:
<!​DOCTYPE​ ​html​>
<​html​ ​lang​=​"en"​>
<​head​>
​<​meta​ ​charset​=​"UTF-8"​>
​<​meta​ ​name​=​"viewport"​ ​content​=​"width=device-width, initial-scale=1.0"​>
​<​title​>​login​</​title​>
</​head​>
<​body​>

<?php
​if​ (​$_SERVER​[​'REQUEST_METHOD'​] === ​'GET'​) {
​if​ (​isset​(​$_GET​[​'message'​])) {
​echo​ ​$_GET​[​'message'​];
}
​echo​ ​'<div>
<form name="form1" action="login.php" method="post">
<input type="text" name="username" id="username"
placeholder="Username">
<input type="password" name="password" id="password"
placeholder="Password">
<input type="submit" value="submit">
</form>
</div>'​;
}

​if​ (​$_SERVER​[​'REQUEST_METHOD'​] === ​'POST'​) {

​extract​(​$_POST​);
​include​(​"database.php"​);

​$rs​=​mysqli_query​(​$conn​,​"select * from user where


username='​$username​'"​);
​if​ (​mysqli_num_rows​(​$rs​)>​0​) {
​echo​ ​"Welcome ​$username​"​;
}
​else​ {
​header​(​"Location:
http://localhost:8082/Exercise8/login.php?message='Credentials are not
valid'"​);
}

​?>

</​body​>
</​html​>

You might also like