ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingติดปัญหาการใช้sessionครับ ช่วยหน่อยค้าบ
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: ติดปัญหาการใช้sessionครับ ช่วยหน่อยค้าบ  (อ่าน 1263 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
Kninekao
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 4



ดูรายละเอียด
« เมื่อ: 07 เมษายน 2023, 10:34:35 »

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

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 4



ดูรายละเอียด
« ตอบ #1 เมื่อ: 07 เมษายน 2023, 10:36:28 »

ไฟล์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);
}
?>


« แก้ไขครั้งสุดท้าย: 07 เมษายน 2023, 10:45:26 โดย Kninekao » บันทึกการเข้า
Kninekao
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 4



ดูรายละเอียด
« ตอบ #2 เมื่อ: 07 เมษายน 2023, 10:36:52 »

ไฟล์user
โค๊ด:
<?php
session_start
();
include &
#39;config/server.php&#39;;


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

?>


<!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/bootstrap@5.0.2/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[&#39;username&#39;];?> 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>

« แก้ไขครั้งสุดท้าย: 07 เมษายน 2023, 10:50:02 โดย Kninekao » บันทึกการเข้า
Kninekao
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 4



ดูรายละเอียด
« ตอบ #3 เมื่อ: 07 เมษายน 2023, 10:37:51 »

ไฟล์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 &
#39;config/server.php&#39;;

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

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

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

        
$context  stream_context_create($options);
        
$verify file_get_contents($urlfalse$context);
        
$captcha_success json_decode($verify);

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


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

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

    
// Initialize cURL
    
$curl curl_init();

    
// Set the cURL options
    
curl_setopt($curlCURLOPT_URL$url);
    
curl_setopt($curlCURLOPT_POSTtrue);
    
curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($data));
    
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
    
curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse); // Set to true if SSL is enabled

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

    
// Check for errors
    
if ($response === false) {
        echo &
#39;cURL error: &#39; . 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>
« แก้ไขครั้งสุดท้าย: 07 เมษายน 2023, 10:40:27 โดย Kninekao » บันทึกการเข้า
หน้า: [1]   ขึ้นบน
พิมพ์