ติดปัญหาการใช้sessionครับ ช่วยหน่อยค้าบ

เริ่มโดย Kninekao, 07 เมษายน 2023, 10:34:35

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

Kninekao

พอดี ผมกำหนดค่าให้sessionแล้วในไฟล์ login_db เรียกดูก็มีค่า แต่พอไปเรียกใช้ใน ไฟล์ user มันไม่มีค่าครับ

Kninekao

#1
ไฟล์login_db
<?php
 session_start
();
include 
'config/server.php';

if (
$_SERVER['REQUEST_METHOD'] == 'POST' && !empty(file_get_contents('php://input'))) {
    
$data = json_decode(file_get_contents('php://input'), true);

    
$username = $data['username'] ?? null;
    
$password = $data['password'] ?? null;

    
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
    
$stmt->bind_param("s", $username);
    
$stmt->execute();
    
$result = $stmt->get_result();

    if (
$result->num_rows > 0) {
        
$row = $result->fetch_assoc();

        if (
$username == $row['username']) {
           
            if (
password_verify($password, $row['password'])) {
                if (
session_status() == PHP_SESSION_ACTIVE) {
                    echo 
'Session is active';
                    
$_SESSION['user_login'] = $row['id'];
                }
                echo 
"<script>
                    $(document).ready(function(){
                        Swal.fire({
                            title: 'success',
                            text: 'เข้าสู่ระบบสำเร็จ!',
                            icon: 'success',
                            timer:1500,
                            showConfirmButton: false
                        }).then(function() {
                            window.location.href = 'user.php';
                        });
          
                    });
                </script>"
;
                exit();
            } else {
                echo 
"<script>
                    $(document).ready(function(){
                        Swal.fire({
                            title: 'error',
                            text: 'รหัสผ่านไม่ถูกต้อง!',
                            icon: 'error',
                            timer:1500,
                            showConfirmButton: false
                        });
                    });
                </script>"
;
                exit();
            }
        } else {
            echo 
"<script>
                $(document).ready(function(){
                    Swal.fire({
                        title: 'error',
                        text: 'ไม่พบยูสเซอร์เนมนี้ในระบบ!',
                        icon: 'error',
                        timer:2000,
                        showConfirmButton: false
                    });
                });
            </script>"
;
        }
    } else {
        echo 
"<script>
            $(document).ready(function(){
                Swal.fire({
                    title: 'error',
                    text: 'ไม่มีข้อมูลในระบบ!',
                    icon: 'error',
                    timer:1500,
                    showConfirmButton: false
                });
            });
        </script>"
;
    }

    
$stmt->close();
    
mysqli_close($conn);
}
?>




Kninekao

#2
ไฟล์user
<?php
session_start
();
include 
'config/server.php';


if (isset(
$_SESSION['user_login'])) {
    echo 
'Session has my_key';
} else {
    echo 
'Session does not have my_key';
}

?>


<!DOCTYPE html>
<html lang="en">

<head>

   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

   <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap" rel="stylesheet">

   <title>Home Page</title>

   <!-- Bootstrap core CSS -->
   <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
   <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

   <!-- Additional CSS Files -->
   <link rel="stylesheet" href="assets/css/fontawesome.css">
   <link rel="stylesheet" href="assets/css/templatemo-cyborg-gaming.css">
   <link rel="stylesheet" href="assets/css/owl.css">
   <link rel="stylesheet" href="assets/css/animate.css">
   <link rel="stylesheet" href="https://unpkg.com/swiper@7/swiper-bundle.min.css" />
   <!--

TemplateMo 579 Cyborg Gaming

[url]https://templatemo.com/tm-579-cyborg-gaming[/url]

-->
</head>

<body>
   <!-- ***** Preloader Start ***** -->
   <div id="js-preloader" class="js-preloader">
       <div class="preloader-inner">
           <span class="dot"></span>
           <div class="dots">
               <span></span>
               <span></span>
               <span></span>
           </div>
       </div>
   </div>
   <!-- ***** Preloader End ***** -->

   <!-- ***** Header Area Start ***** -->
   <header class="header-area header-sticky">
       <div class="container">
           <div class="row">
               <div class="col-12">
                   <nav class="main-nav">
                       <!-- ***** Logo Start ***** -->
                       <a href="user.php" class="logo">
                           <img src="assets/images/logo.png" alt="">
                       </a>
                       <!-- ***** Logo End ***** -->
                       <!-- ***** Menu Start ***** -->
                       <ul class="nav">
                           <li><a href="user.php" class="active">Home</a></li>
                           <li><a href="browse.html">Browse</a></li>
                           <li><a href="details.html">Details</a></li>
                           <li><a href="streams.html">Streams</a></li>
                           <li><a href="profile.php" class="active">Profile <img src="assets/images/profile-header.jpg" alt=""></a></li>
                       </ul>
                       <a class='menu-trigger'>
                           <span>Menu</span>
                       </a>
                       <!-- ***** Menu End ***** -->
                   </nav>
               </div>
           </div>
       </div>
   </header>
   <!-- ***** Header Area End ***** -->

   <div class="container">
       <div class="row">
           <div class="col-lg-12">
               <div class="page-content">

                   <!-- ***** Banner Start ***** -->
                   <div class="main-banner">
                       <div class="row">
                           <div class="col-lg-7">
                               <div class="header-text">
                                   <h6>Welcome <?php echo $result['username'];?> To Top 10 Toxic Game</h6>
                                   <h4><em>Browse</em> Our Popular Toxic Game</h4>
                                   <div class="main-button">
                                       <a href="browse.html">Browse Now</a>
                                   </div>
                               </div>
                           </div>
                       </div>
                   </div>
                   <!-- ***** Banner End ***** -->

                   <!-- ***** Most Popular Start ***** -->
                   <div class="most-popular">
                       <div class="row">
                           <div class="col-lg-12">
                               <div class="heading-section">
                                   <h4><em>Most Popular</em> Right Now</h4>
                               </div>
                               <div class="row">
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-01 1.svg" alt="">
                                           <h4>World of Warcraft<br><span>Moba</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 5</li>
                                               <li><i class="fa fa-user-circle"></i> 10M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-02 1.svg" alt="">
                                           <h4>Rocket League<br><span>SPORT</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 5</li>
                                               <li><i class="fa fa-user-circle"></i> 30M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-03 1.svg" alt="">
                                           <h4>Roblox<br><span>Sandbox</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 4.85</li>
                                               <li><i class="fa fa-user-circle"></i> 150M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-04 1.svg" alt="">
                                           <h4>Fortnite<br><span>Battle royale</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 4.85</li>
                                               <li><i class="fa fa-user-circle"></i> 78.3M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-05 1.svg" alt="">
                                           <h4>Dota 2<br><span>Moba</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 4.85</li>
                                               <li><i class="fa fa-user-circle"></i> 1.29M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-06 1.svg" alt="">
                                           <h4>CS:GO<br><span>FPS</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 4.8</li>
                                               <li><i class="fa fa-user-circle"></i> 1.3M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-07 1.svg" alt="">
                                           <h4>Apex Legends<br><span>Battle royale</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 4.8</li>
                                               <li><i class="fa fa-user-circle"></i> 500k</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-08 1.svg" alt="">
                                           <h4>Among Us<br><span>Party</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 4.75</li>
                                               <li><i class="fa fa-user-circle"></i> 500M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-09 1.svg" alt="">
                                           <h4>Minecraft<br><span>Sandbox</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 4.75</li>
                                               <li><i class="fa fa-user-circle"></i> 238M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-3 col-sm-6">
                                       <div class="item">
                                           <img src="assets/images/popular-10 1.svg" alt="">
                                           <h4>League of Legends<br><span>Moba</span></h4>
                                           <ul>
                                               <li><i class="fa fa-star"></i> 4.75</li>
                                               <li><i class="fa fa-user-circle"></i> 150M</li>
                                           </ul>
                                       </div>
                                   </div>
                                   <div class="col-lg-12">
                                       <div class="main-button">
                                           <a href="browse.html">Discover Popular</a>
                                       </div>
                                   </div>
                               </div>
                           </div>
                       </div>
                   </div>
                   <!-- ***** Most Popular End ***** -->

               </div>
           </div>
       </div>
   </div>

   <footer>
       <div class="container">
           <div class="row">
               <div class="col-lg-12">
                   <p>Copyright © 2036 <a href="#">Cyborg Gaming</a> Company. All rights reserved.

                       <br>Design: <a href="https://templatemo.com" target="_blank" title="free CSS templates">TemplateMo</a>
                       Distributed By <a href="https://themewagon.com" target="_blank">ThemeWagon</a>
                       <br>Reference: <a href="https://www.adl.org/resources/report/hate-no-game-harassment-and-positive-social-experiences-online-games-2021" target="_blank" title="free CSS templates">ADL</a>
                   </p>

               </div>
           </div>
       </div>
   </footer>


   <!-- Scripts -->
   <!-- Bootstrap core JavaScript -->
   <script src="vendor/jquery/jquery.min.js"></script>
   <script src="vendor/bootstrap/js/bootstrap.min.js"></script>

   <script src="assets/js/isotope.min.js"></script>
   <script src="assets/js/owl-carousel.js"></script>
   <script src="assets/js/tabs.js"></script>
   <script src="assets/js/popup.js"></script>
   <script src="assets/js/custom.js"></script>
</body>

</html>



Kninekao

#3
ไฟล์login ที่ส่งข้อมูลให้ login ใช้ cURL
<script src="https://code.jquery.com/jquery-3.6.4.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<?php
session_start
();
include 
'config/server.php';

// ตั้งค่าข้อมูลสำหรับส่งไปยัง URL
if (isset($_POST['login'])) {
    
// Verify reCAPTCHA
    
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
        
$captcha = $_POST['g-recaptcha-response'];
        
$secretKey = "6LfDDlElAAAAAFPQpHi8t57yCaBDLzLRqmRYm0fw"; 
        
$ip = $_SERVER['REMOTE_ADDR'];

        
// Verify the captcha response
        
$url = '[url]https://www.google.com/recaptcha/api/siteverify';[/url]
        
$data = array(
            
'secret' => $secretKey,
            
'response' => $captcha,
            
'remoteip' => $ip
        
);

        
$options = array(
            
'http' => array (
                
'method' => 'POST',
                
'header' => 'Content-Type: application/x-www-form-urlencoded',
                
'content' => http_build_query($data)
            )
        );

        
$context  = stream_context_create($options);
        
$verify = file_get_contents($url, false, $context);
        
$captcha_success = json_decode($verify);

        if (
$captcha_success->success == false) {
            echo 
"<script>
            $(document).ready(function(){
                Swal.fire({
                    title: 'error',
                    text: 'กรุณายืนยันrecaptcha!',
                    icon: 'error',
                    timer:2000,
                    showConfirmButton: false
                }).then(function() {
                    window.location.href = 'login.php';
                });
  
            });
        </script>"
;
            exit;
        }
    } else {
        echo 
"<script>
            $(document).ready(function(){
                Swal.fire({
                    title: 'error',
                    text: 'กรุณายืนยันrecaptcha!',
                    icon: 'error',
                    timer:2000,
                    showConfirmButton: false
                }).then(function() {
                    window.location.href = 'login.php';
                });
  
            });
        </script>"
;
        exit;
    }


    
// Set the endpoint URL
    
$url = "https://localhost/new/login_db.php";

    
// Set the POST data
    
$data = array(
        
'username' => $_POST['username'],
        
'password' => $_POST['password'],
    );

    
// Initialize cURL
    
$curl = curl_init();

    
// Set the cURL options
    
curl_setopt($curl, CURLOPT_URL, $url);
    
curl_setopt($curl, CURLOPT_POST, true);
    
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
    
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Set to true if SSL is enabled

    // Execute the cURL request
    
$response = curl_exec($curl);

    
// Check for errors
    
if ($response === false) {
        echo 
'cURL error: ' . curl_error($curl);
    }

    
// Close the cURL session
    
curl_close($curl);

    
// Output the response
    
echo $response;
}
?>



<!DOCTYPE html>
<!-- Coding By CodingNepal - youtube.com/codingnepal -->
<html lang="en" dir="ltr">

<head>
   <meta charset="utf-8">
   <title>Login Page</title>
   <link rel="stylesheet" href="style.css">
   <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>

<body>
   <div class="center">
       <h1>SIGN IN </h1>
       <form method="post">
           <div class="txt_field">
               <input type="text" name="username" required>
               <span></span>
               <label for="username">Username</label>
           </div>

           <div class="txt_field">
               <input type="password" name="password" required>
               <span></span>
               <label for="password">Password</label>
           </div>
           <div>
               <div class="g-recaptcha" data-sitekey="6LfDDlElAAAAALmlB1Y2sRKvbCe3SUIVu3ySohWa"></div>
           </div>
           <input type="submit" name="login" value="Sign In">
           <div class="signup_link">
               <p>Have an Account? <a href="register.php">Sign Up</a></p>
           </div>
       </form>
   </div>
</body>

</html>