domain = $_SERVER['HTTP_HOST']; $this->token = $_COOKIE['authToken'] ?? ''; // Get Bear Token from cookie $this->args = [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => "Bearer $this->token", ] ]; } public function get_laravel_auth_status() { // 設定 Laravel API URL $domain = $_SERVER['HTTP_HOST']; $api_url = "http://$this->domain/student/api/user"; // Get Bear Token from cookie $token = $_COOKIE['authToken'] ?? ''; // 如果需要帶上 cookie,讓 Laravel 認證機制能讀取登入資訊 $args = [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => "Bearer $this->token", ], // 將當前瀏覽器的 cookie 帶入,前提是兩者可以共用 cookie 或有其他認證方式 ]; $response = wp_remote_get( $api_url, $args ); if ( is_wp_error( $response ) ) { return 'Error: ' . $response->get_error_message(); } // die('
' . print_r($response, true) . '
'); $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); $school_no = null; $valid_date = null; date_default_timezone_set( 'Asia/Taipei' ); $time = date( 'Y-m-d' ); if ( ! empty( $data['school_no'] ) && ! empty( $data['valid_date'] ) ) { $school_no = $data['school_no']; $valid_date = $data['valid_date']; } if ( $school_no && $valid_date > $time ) { $_SESSION['aia_student_school_no'] = $school_no; // echo $_SESSION['aia_student_school_no']; } else { unset( $_SESSION['aia_student_school_no'] ); } } public function student_logout() { // die('test'); // $api_url = "http://$this->domain/student/api/logout"; // $response = wp_remote_post($api_url, $this->args); // if (is_wp_error($response)) { // return 'Error: ' . $response->get_error_message(); // } unset( $_SESSION['aia_student_school_no'] ); session_destroy(); wp_redirect( "http://$this->domain/student/logout" ); exit; // redirect("http://$this->domain/student/logout"); // die('
' . print_r($response, true) . '
'); } } session_start(); $LaravelAuth = new LaravelAuthStatus(); if ( ! isset( $_SESSION['aia_student_school_no'] ) ) { $LaravelAuth->get_laravel_auth_status(); } if ( isset( $_GET['logout'] ) ) { $LaravelAuth->student_logout(); } // die('test_end'); // die();