Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

   
  •           
  •   
  •           
  •   
  •       
  •           
  •   
  •   
  •   
  •   
  • Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Db_conection.php

    1. Giới thiệu
    2.  
    3.  
    4.  
    5.  
    6.  
    7.   
    8. $dbcon=mysqli_connect("localhost","root","");  =mysqli_connect("localhost","root","");  
    9. mysqli_select_db($dbcon,"users");  $dbcon,"users");  
    10. ?>  

    Delete.php

    1. Giới thiệu
    2.  
    3.  
    4.  
    5.  
    6.  
    7.   
    8. include("database/db_conection.php");  ("database/db_conection.php");  
    9. $delete_id=$_GET['del'];  =$_GET['del'];  
    10. $delete_query="delete  from users WHERE id='$delete_id'";  ="delete  from users WHERE id='$delete_id'";  
    11. $run=mysqli_query($dbcon,$delete_query);  =mysqli_query($dbcon,$delete_query);  
    12. if($run)  ($run)  
    13. {  
    14.   
    15.     echo "window.open('view_users.php?deleted=user has been deleted','_self')";  echo "";  
    16. }  
    17.   
    18. ?>  


    Mã PHP bên dưới là login_successful.php thu được sau khi đăng nhập người dùng, trong trang này tôi muốn hiển thị 'tên người dùng' của anh ấy và liên kết đăng xuất

    
    
    
    
    
    Welcome $myusername //here i want to display logged in user's name
    Login Successful
    
    
    
    

    Làm thế nào tôi nên đặt liên kết đăng xuất trong trang này mà không cần sử dụng tệp logout.php khác.

    Đã hỏi ngày 13 tháng 11 năm 2012 lúc 10:41Nov 13, 2012 at 10:41

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Nagendra Raonagendra RaoNagendra Rao

    6.8565 huy hiệu vàng49 Huy hiệu bạc89 Huy hiệu đồng5 gold badges49 silver badges89 bronze badges

    5

    Tại sao sử dụng một trang khác để đăng xuất? Làm như thế này

    
    

    Đã trả lời ngày 13 tháng 11 năm 2012 lúc 10:45Nov 13, 2012 at 10:45

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Ông Alienmr. Người ngoài hành tinhMr. Alien

    150K33 Huy hiệu vàng290 Huy hiệu bạc274 Huy hiệu đồng33 gold badges290 silver badges274 bronze badges

    5

    Bạn phải kiểm tra phiên wheter có tên người dùng của anh ấy và sau đó hiển thị, một cái gì đó như:

    session_start();
    if(isset($_SESSION['username'])){
       echo "Hello, " . $_SESSION['username']);
       echo "Logout"
    }
    

    Đã trả lời ngày 13 tháng 11 năm 2012 lúc 10:47Nov 13, 2012 at 10:47

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Rodrigo Diasrodrigo DiasRodrigo Dias

    1.2832 Huy hiệu vàng13 Huy hiệu bạc17 Huy hiệu đồng2 gold badges13 silver badges17 bronze badges

    Bạn luôn có thể gọi session_destroy () để (đoán xem) phá hủy các phiên của bạn! Từ hướng dẫn:

    session_destroy () phá hủy tất cả các dữ liệu được liên kết với phiên hiện tại. Nó không hủy bỏ bất kỳ biến toàn cầu nào liên quan đến phiên hoặc mở cookie phiên.

    Quan trọng hơn việc sử dụng session_destroy() là đảm bảo bạn đã đặt lại cookie (nếu có được sử dụng) bằng cách đặt thời gian một giờ trở lại: time() - 3600, như:

    setcookie ("YourCookieName", "", time() - 3600);
    

    Đã trả lời ngày 13 tháng 11 năm 2012 lúc 10:49Nov 13, 2012 at 10:49

    Bruno Vieirabruno VieiraBruno Vieira

    3,8641 Huy hiệu vàng23 Huy hiệu bạc35 Huy hiệu đồng1 gold badge23 silver badges35 bronze badges

    Tôi giả sử ở đây rằng bạn đã quen thuộc với HTML và CSS.Tôi đã nhận xét mã với thông tin cần thiết trong phần chính của mã.Vì vậy, bạn có thể hiểu dễ dàng.Tôi cũng đã đính kèm mã nguồn để bạn có thể tải xuống và sử dụng nó.Hãy bắt đầu.

    Ghi chú

    Tôi đã đăng tập lệnh đăng ký PHP này trên blog cá nhân của tôi.Bạn cũng có thể kiểm tra nó.

    Sau đây là những điểm chúng tôi sẽ học trong bài viết này,

    Tạo một cơ sở dữ liệu trong phpmyadmin.

    1. Tạo kết nối với cơ sở dữ liệu MySQL.
    2. Chèn, xóa và xem dữ liệu từ cơ sở dữ liệu MySQL.
    3. Một số thành phần bootstrap.
    4. Phiên trong PHP.
    5. Tập tin và IDE

    Sự đăng ký

    1. Đăng nhập
    2. Đăng xuất
    3. Chào mừng
    4. Tôi đang sử dụng phpstorm cho khung mã hóa và bootstrap để phát triển mặt trước.
    5. Admin_login
    6. View_users
    7. Db_conection
    8. Delete.php

    Tạo nên cơ sở dữ liệu

    Tạo bảng

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Tạo các cột trong bảng người dùng

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Bảng quản trị viên

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Tạo các cột quản trị

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Tạo các cột quản trị

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Registration.php

    1.   
    2.   "en">  
    3.       "UTF-8">  
    4.       "text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">  
    5.     Registration  
    6.   
    7.   
    8.     .login-panel {  
    9.         margin-top: 150px;  
    10.   
    11.   
    12.   
    13.   
    14.   class="container">  
    15.       class="row">  
    16.           class="col-md-4 col-md-offset-4">  
    17.               class="login-panel panel panel-success">  
    18.                   class="panel-heading">  
    19.                     Registration  class="panel-title">Registration  
    20.                   
    21.                   class="panel-body">  
    22.                       "form" method="post" action="registration.php">  
    23.                           
    24.                               class="form-group">  
    25.                                   class="form-control" placeholder="Username" name="name" type="text" autofocus>  
    26.                               
    27.   
    28.                               class="form-group">  
    29.                                   class="form-control" placeholder="E-mail" name="email" type="email" autofocus>  
    30.                               
    31.                               class="form-group">  
    32.                                   class="form-control" placeholder="Password" name="pass" type="password" value="">  
    33.                               
    34.   
    35.   
    36.                               class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >  
    37.   
    38.                           
    39.                       
    40.                     Already registered ? Login here  "login.php">Login here  
    41.                   
    42.               
    43.           
    44.       
    45.   
    46.   
    47.   
    48.   
    49.   
    50.   
    51. } & nbsp;
    52.   
    53. include("database/db_conection.php");  "database/db_conection.php");  
    54. if(isset($_POST['register']))  (isset($_POST['register']))  
    55. {  
    56.     $user_name=$_POST['name'];  'name'];  
    57.     $user_pass=$_POST['pass'];  'pass'];  
    58.     $user_email=$_POST['email'];  'email'];  
    59.   
    60.   
    61.     if($user_name=='')  if($user_name=='')  
    62.     {  
    63.             
    64.         echo"alert('Please enter the name')";  "";  
    65. exit();    
    66.     }  
    67.   
    68.     if($user_pass=='')  if($user_pass=='')  
    69.     {  
    70.         echo"alert('Please enter the password')";  "";  
    71. exit();  
    72.     }  
    73.   
    74.     if($user_email=='')  if($user_email=='')  
    75.     {  
    76.         echo"alert('Please enter the email')";  "";  
    77.     exit();  
    78.     }  
    79.   
    80.     $check_email_query="select * from users WHERE user_email='$user_email'";  "select * from users WHERE user_email='$user_email'";  
    81.     $run_query=mysqli_query($dbcon,$check_email_query);  
    82.   
    83.     if(mysqli_num_rows($run_query)>0)  if(mysqli_num_rows($run_query)>0)  
    84.     {  
    85. echo "alert('Email $user_email is already exist in our database, Please try another one!')";  "";  
    86. exit();  
    87.     }  
    88.   
    89.     $insert_user="insert into users (user_name,user_pass,user_email) VALUE ('$user_name','$user_pass','$user_email')";  "insert into users (user_name,user_pass,user_email) VALUE ('$user_name','$user_pass','$user_email')";  
    90.     if(mysqli_query($dbcon,$insert_user))  if(mysqli_query($dbcon,$insert_user))  
    91.     {  
    92.         echo"window.open('welcome.php','_self')";  "";  
    93.     }  
    94. } & nbsp; 
    95. ?>  

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Login.php

    1. } & nbsp;
    2. session_start();    
    3.   
    4. ?>  
    5.   
    6.   
    7.   "en">  
    8.       "UTF-8">  
    9.       "text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">  
    10.     Login  
    11.   
    12.   
    13.     .login-panel {  
    14.         margin-top: 150px;  
    15.   
    16.   
    17.   
    18.   
    19.   
    20.   class="container">  
    21.       class="row">  
    22.           class="col-md-4 col-md-offset-4">  
    23.               class="login-panel panel panel-success">  
    24.                   class="panel-heading">  
    25.                     Sign In  class="panel-title">Sign In  
    26.                   
    27.                   class="panel-body">  
    28.                       "form" method="post" action="login.php">  
    29.                           
    30.                               class="form-group"  >  
    31.                                   class="form-control" placeholder="E-mail" name="email" type="email" autofocus>  
    32.                               
    33.                               class="form-group">  
    34.                                   class="form-control" placeholder="Password" name="pass" type="password" value="">  
    35.                               
    36.   
    37.   
    38.                                   class="btn btn-lg btn-success btn-block" type="submit" value="login" name="login" >  
    39.   
    40.                               or input when using this as a form -->  
    41.                             "index.html" class="btn btn-lg btn-success btn-block">Login -->  
    42.                           
    43.                       
    44.                   
    45.               
    46.           
    47.       
    48.   
    49.   
    50.   
    51.   
    52.   
    53.   
    54. include("database/db_conection.php");  ("database/db_conection.php");  
    55.   
    56. if(isset($_POST['login']))  (isset($_POST['login']))  
    57. {  
    58.     $user_email=$_POST['email'];  $user_email=$_POST['email'];  
    59.     $user_pass=$_POST['pass'];  $user_pass=$_POST['pass'];  
    60.   
    61.     $check_user="select * from users WHERE user_email='$user_email'AND user_pass='$user_pass'";  $check_user="select * from users WHERE user_email='$user_email'AND user_pass='$user_pass'";  
    62.   
    63.     $run=mysqli_query($dbcon,$check_user);  $run=mysqli_query($dbcon,$check_user);  
    64.   
    65.     if(mysqli_num_rows($run))  if(mysqli_num_rows($run))  
    66.     {  
    67.         echo "window.open('welcome.php','_self')";  echo "";  
    68.   
    69.         $_SESSION['email']=$user_email;  $_SESSION['email']=$user_email;  
    70.   
    71.     }  
    72.     else  else  
    73.     {  
    74.       echo "alert('Email or password is incorrect!')";  echo "";  
    75.     }  
    76. }  
    77. ?>  

    Hướng dẫn logout code in php without session - mã đăng xuất trong php mà không cần phiên

    Logout.php

    1.  
    2.  
    3.  
    4.  
    5.  
    6.   
    7.   
    8. session_start();    
    9. session_destroy();  
    10. header("Location: login.php");  "Location: login.php");  
    11. ?>  

    Welcome.php

    1. session_start();  
    2.   
    3. if(!$_SESSION['email'])  (!$_SESSION['email'])  
    4. {  
    5.   
    6.     header("Location: login.php");  "Location: login.php");  
    7. }  
    8.   
    9. ?>  
    10.   
    11.   
    12.   
    13.   
    14.       
    15.         Registration  
    16.       
    17.   
    18.   
    19.   
    20. Welcome  
    21. echo $_SESSION['email'];   $_SESSION['email'];  
    22. ?>  
    23. Logout here   "logout.php">Logout here   
    24.   
    25.   
    26.   

    Admin_login.php

    1.   
    2.   "en">  
    3.       "UTF-8">  
    4.       "text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">  
    5.     Admin Login  
    6.   
    7.   
    8.     .login-panel {  
    9.         margin-top: 150px;  
    10.   
    11.   
    12.   
    13.   
    14.   
    15.   class="container">  
    16.       class="row">  
    17.           class="col-md-4 col-md-offset-4">  
    18.               class="login-panel panel panel-success">  
    19.                   class="panel-heading">  
    20.                     Sign In  class="panel-title">Sign In  
    21.                   
    22.                   class="panel-body">  
    23.                       "form" method="post" action="admin_login.php">  
    24.                           
    25.                               class="form-group"  >  
    26.                                   class="form-control" placeholder="Name" name="admin_name" type="text" autofocus>  
    27.                               
    28.                               class="form-group">  
    29.                                   class="form-control" placeholder="Password" name="admin_pass" type="password" value="">  
    30.                               
    31.                               class="btn btn-lg btn-success btn-block" type="submit" value="login" name="admin_login" >  
    32.                           
    33.                       
    34.                   
    35.               
    36.           
    37.       
    38.   
    39.   
    40.   
    41.   
    42.   
    43. View_users.php
    44.  
    45.  
    46.  
    47.  
    48.  
    49.   
    50. include("database/db_conection.php");  "database/db_conection.php");  
    51.   
    52. if(isset($_POST['admin_login']))  (isset($_POST['admin_login']))  
    53. {  
    54.     $admin_name=$_POST['admin_name'];  'admin_name'];  
    55.     $admin_pass=$_POST['admin_pass'];  'admin_pass'];  
    56.   
    57.     $admin_query="select * from admin where admin_name='$admin_name' AND admin_pass='$admin_pass'";  "select * from admin where admin_name='$admin_name' AND admin_pass='$admin_pass'";  
    58.   
    59.     $run_query=mysqli_query($dbcon,$admin_query);  
    60.   
    61.     if(mysqli_num_rows($run_query)>0)  if(mysqli_num_rows($run_query)>0)  
    62.     {  
    63.   
    64.         echo "window.open('view_users.php','_self')";  "";  
    65.     }  
    66.     else {echo"alert('Admin Details are incorrect..!')";}  else {echo"";}  
    67.   
    68. }  
    69.   
    70. ?>  

    & nbsp;& nbsp; & nbsp;} & nbsp; & nbsp;

    1.   
    2.   "en">  
    3.       "UTF-8">  
    4.        "text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">   
    5.     View Users  
    6.   
    7.   
    8.     .login-panel {  
    9.         margin-top: 150px;  
    10.     }  
    11.     .table {  
    12.         margin-top: 50px;  
    13. & nbsp;& nbsp; & nbsp;} & nbsp; & nbsp; 
    14.   
    15.   
    16.   
    17.   
    18.   class="table-scrol">  
    19.     All the Users  "center">All the Users  
    20.   
    21.   class="table-responsive">  
    22.       class="table table-bordered table-hover table-striped" style="table-layout: fixed">  
    23.           
    24.   
    25.           
    26.   
    27.             User Id  
    28.             User Name  
    29.             User E-mail  
    30.             User Pass  
    31.             Delete User  
    32.           
    33.           
    34.   
    35.         
    36.         include("database/db_conection.php");  "database/db_conection.php");  
    37.         $view_users_query="select * from users";  "select * from users";  
    38.         $run=mysqli_query($dbcon,$view_users_query);    
    39.   
    40.         while($row=mysqli_fetch_array($run))  while($row=mysqli_fetch_array($run))  
    41.         {  
    42.             $user_id=$row[0];  
    43.             $user_name=$row[1];  
    44.             $user_email=$row[2];  
    45.             $user_pass=$row[3];  
    46.   
    47.         ?>  
    48.   
    49.           
    50.   in the table -->  
    51.               
    52.               
    53.               
    54.               
    55.             Delete   "delete.php?del=">class="btn btn-danger">Delete