Skip to content

Commit f8b81bc

Browse files
committed
added chapter 7
1 parent 0d11f98 commit f8b81bc

File tree

9 files changed

+189
-52
lines changed

9 files changed

+189
-52
lines changed

public/chapter7/dbconnect.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

public/chapter7/first_session.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
// Start the session
3+
session_start();
4+
?>
5+
<!DOCTYPE html>
6+
<html>
7+
<body>
8+
9+
<?php
10+
// Set session variables
11+
$_SESSION["firstname"] = "Foo";
12+
$_SESSION["username"] = "barFoo";
13+
echo "Session variables are set.";
14+
?>
15+
16+
</body>
17+
</html>

public/chapter7/first_session2.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
// Start the session
3+
session_start();
4+
?>
5+
<!DOCTYPE html>
6+
<html>
7+
<body>
8+
9+
<?php
10+
// Set session variables
11+
$_SESSION["firstname"] = "Foo";
12+
$_SESSION["username"] = "barFoo";
13+
echo "Session variables are set.";
14+
echo "<br />";
15+
var_dump($_SESSION);
16+
?>
17+
18+
</body>
19+
</html>

public/chapter7/login.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
// to Start a PHP session
3+
session_start();
4+
?>
5+
<html>
6+
<body>
7+
<div class="container">
8+
<form method="post" action="">
9+
<div id="div_login">
10+
<h1>Login</h1>
11+
<div>
12+
<input type="text" class="textbox" id="first_name" name="first_name" placeholder="first_name" />
13+
</div>
14+
<div>
15+
<input type="password" class="textbox" id="password" name="password" placeholder="Password"/>
16+
</div>
17+
<div>
18+
<input type="submit" value="Submit" name="submit" id="submit" />
19+
</div>
20+
</div>
21+
</form>
22+
</div>
23+
24+
<?php
25+
// DB Host name
26+
$host = "mysql-db";
27+
28+
// DB User
29+
$user = "user";
30+
31+
// DB Password
32+
$password = "pass";
33+
34+
// Database name
35+
$db = "beginningPHP";
36+
37+
$connection = mysqli_connect($host, $user, $password, $db);
38+
39+
// If the connection fails
40+
if (!$connection) {
41+
42+
// Display message and terminate script
43+
die("Connection failed: " . mysqli_connect_error());
44+
}
45+
46+
// If the submit button is pressed
47+
if(isset($_POST['submit'])){
48+
49+
// Escape special characters in a string
50+
$first_name = mysqli_real_escape_string($connection, $_POST['first_name']);
51+
$password = mysqli_real_escape_string($connection, $_POST['password']);
52+
53+
// If username and password are not empty
54+
if ($first_name != "" && $password != ""){
55+
56+
// Query database to find user with matching username and password
57+
$query = "select count(*) as countUser from users where first_name='".$first_name."' and password='".$password."'";
58+
59+
// Store query result
60+
$result = mysqli_query($connection, $query);
61+
62+
// Fetch row as associative array
63+
$row = mysqli_fetch_array($result);
64+
65+
// Get number of rows
66+
$count = $row['countUser'];
67+
68+
// If number of row is more than zero
69+
if($count > 0){
70+
71+
// Set matched user as current user
72+
$_SESSION['first_name'] = $first_name;
73+
$_SESSION['timestamp'] = date("h:i:sa");
74+
75+
// Display success message
76+
echo "You are logged in!";
77+
if (isset($_SESSION)) {
78+
echo "<br /><br />";
79+
print_r($_SESSION);
80+
}
81+
82+
// Else if number of row is less than zero
83+
} else {
84+
85+
// Display failed message
86+
echo "Error! Invalid first_name and password.";
87+
}
88+
89+
}
90+
91+
}
92+
?>
93+
</body>
94+
</html>

public/chapter7/remove_session.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
session_start();
3+
?>
4+
<!DOCTYPE html>
5+
<html>
6+
<body>
7+
8+
<?php
9+
echo "Here are the variables:<br />";
10+
var_dump($_SESSION);
11+
echo "<br /><br />";
12+
// remove all session variables
13+
session_unset();
14+
echo "Here are the variables after session_unset:<br />";
15+
var_dump($_SESSION);
16+
echo "<br /><br />";
17+
18+
// destroy the session
19+
session_destroy();
20+
echo "Here are the variables after session_destroy:<br />";
21+
var_dump($_SESSION);
22+
echo "<br /><br />";
23+
?>
24+
25+
</body>
26+
</html>

public/chapter7/seedDB.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22

33
require_once('db.php');
44
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_DATABASE);
5-
$query = "Select * from users";
6-
$result = $mysqli->query($query);
7-
if (!$result) {
8-
echo 'Creating table "USERS"';
95
$query = 'create table IF NOT EXISTS users(
106
id INT NOT NULL AUTO_INCREMENT,
117
first_name VARCHAR(100) NOT NULL,
128
last_name VARCHAR(40) NOT NULL,
9+
password VARCHAR(10) NOT NULL,
1310
created DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP default current_timestamp,
1411
PRIMARY KEY ( id )
1512
)';
1613
$result = $mysqli->query($query);
17-
echo 'Seeing Users into table..1';
18-
$query = "INSERT INTO users (first_name,last_name) values ('tom','hanks')";
19-
$result = $mysqli->query($query);
20-
echo '..2';
21-
$query = "INSERT INTO users (first_name,last_name) values ('billy','mitchell')";
22-
$result = $mysqli->query($query);
23-
echo '..3<br />';
24-
$query = "INSERT INTO users (first_name,last_name) values ('mega','man')";
25-
$result = $mysqli->query($query);
26-
echo 'Users added<br />';
2714

2815
$query = "Select * from users";
2916
$result = $mysqli->query($query);
30-
while ($row = $result->fetch_assoc()) {
31-
print $row['id'].' - '.$row['first_name'].' - '.$row['last_name'].' - '.$row['created'].'<br />';
17+
if ($result->num_rows == 0) {
18+
echo 'Seeing Users into table..1';
19+
$query = "INSERT INTO users (first_name,last_name,password) values ('tom','hanks','1234')";
20+
$result = $mysqli->query($query);
21+
echo '..2';
22+
$query = "INSERT INTO users (first_name,last_name,password) values ('billy','mitchell','1234')";
23+
$result = $mysqli->query($query);
24+
echo '..3<br />';
25+
$query = "INSERT INTO users (first_name,last_name,password) values ('mega','man','1234')";
26+
$result = $mysqli->query($query);
27+
echo 'Users added<br />';
28+
} else {
29+
echo "data already added<br /><br />";
3230
}
3331

34-
} else {
35-
echo 'No Need';
32+
$query = "Select * from users";
33+
$result = $mysqli->query($query);
34+
while ($row = $result->fetch_assoc()) {
35+
print $row['id'].' - '.$row['first_name'].' - '.$row['last_name'].' - '. $row['password'].' - '.$row['created'].'<br />';
3636
}

public/chapter7/session_test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
session_start();
3+
?>
4+
<!DOCTYPE html>
5+
<html>
6+
<body>
7+
8+
<?php
9+
// Echo session variables that were set on previous page
10+
echo "Firstname is " . $_SESSION["firstname"] . ".<br>";
11+
echo "Username is " . $_SESSION["username"] . ".";
12+
?>
13+
14+
</body>
15+
</html>

public/chapter7/showUsers.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

public/chapter7/sql.sql

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)