|
dbconnect
|
|
|
lesson44.php
|
|
|
connect to database
|
|
|
<?php
|
|
|
$host = 'localhost';
|
|
|
$user = 'root';
|
|
|
$pass = '';
|
|
|
$dbname = 'lesson44';
|
|
|
|
|
|
if($mycon = @mysqli_connect($host, $user, $pass)){
|
|
|
|
|
|
if(@mysqli_select_db($mycon, $dbname)){
|
|
|
//echo 'connected to the DB';
|
|
|
} else {
|
|
|
//echo 'DB connection failed';
|
|
|
} }
|
|
|
|
|
|
else {
|
|
|
//echo 'server connection failed';
|
|
|
}
|
|
|
|
|
|
|
|
|
?>
|
|
|
core.php
|
|
|
<?php
|
|
|
ob_start();
|
to start the header we wiil use this
|
|
//to use header we have to start this
|
|
|
session_start();
|
to save cookies we have to start sessions,logged in till log
out,using this only
|
|
$current_file =
$_SERVER['SCRIPT_NAME'];
|
using this we can get the url of the present page
|
|
//IT WILL Bcarry basic data - it will give the url withoud
domainname /login.php
|
|
|
|
|
|
function loggedin(){
|
implement logged in function, we can call this funtuin by
placing this page and call this function
|
|
if (isset($_SESSION['user_id']) &&
!empty($_SESSION['user_id'])){ return true;}
|
if we got the value in user id , any rows are matched, matched
means not empty , so it returns
|
|
else{return false;}
|
|
|
}
|
|
|
|
|
|
?>
|
|
|
login.php
|
|
|
<?php
|
|
|
if(isset($_POST['username'])&&isset($_POST['password']
) ){
|
user name and password is submitted
|
|
$username = $_POST ['username'];
|
load that entered username and password to some variables
|
|
$password = $_POST['password'];
|
|
|
$pass_hash = md5($password);
|
convert that entered pasword to md5
|
|
|
|
|
if(!empty($username) &&!empty($password)){
|
username values and password values are not empty
|
|
$query = "SELECT `id` FROM `users` WHERE `username` =
'$username' AND `password` = '$pass_hash' ";
|
//entered username and password , match using this query, if
match, it writes how many number of rows match,if not match it will write 0
|
|
|
|
|
if ($query_run = mysqli_query($mycon, $query ) ) {
|
//mycon is not there in this
page and we not used this in this database, so we have to understand
we are not using this page directly, we r creting template for another page,
at that we used reuqire
|
|
|
|
|
$num_rows =
mysqli_num_rows($query_run);
|
get the how many matched rows, u can see how many rows are
matched using echo
|
|
if ($num_rows == 0){echo 'invalid username and password';}
|
if rows are not matched , enter valid password
|
|
|
// 0 means no row is matched, so entered thing is fault
|
|
else if ($num_rows == 1){$row = mysqli_fetch_row($query_run);
echo $id = $row[0];}
|
if rows are mached , ur id is matched and it shows valid things
|
|
|
// it will print whats the id matched
|
|
|
//if enetered username and password is matched with database, it
generates some rows or convert ted
arrays and prints that array using array access
|
|
|
//after matched we have to start session
|
|
$_SESSION['user_id'] = $id;
|
write the array data to sesssion
|
|
//session variable is set
|
|
|
// close browser, loggged out means session variable destroyed
|
|
|
header('Location: lesson47.php');
|
print the logged in using this in header
|
|
|
|
|
|
|
|
}
|
in this page we are using , we not declared some variable in
this page, eventhough we willl use, because, we run another page , we access
that vairable via that page
|
|
else {echo 'please enter username and password!';}
|
|
|
?>
|
|
|
<form action="<?php echo $current_file ?>"
method="post">
|
|
|
username: <input type="text"
name="username"> <br><br>
|
|
|
password: <input type="password"
name="password"> <br> <br>
|
|
|
<input type="submit" value="log in">
|
|
|
</form>
|
|
| @mysqli_connect |
| @mysqli_select_db |
| $_SERVER['SCRIPT_NAME'] |
| isset |
| ($_SESSION['user_id'] |
| isset |
| $_POST |
| IF IF IF ELSEIF ELSE |
| !EMPTY |
| SELECT FROM WHERE |
| mysqli_query |
| mysqli_num_rows |
| $_SESSION['user_id'] |
| header |
No comments:
Post a Comment