所以我有一个login.php来启动会话并设置'login',然后将用户重定向到一个html页面(pTable.html),如下所示:
<?ph //starting session session_start(); //connecting to database and such echo $_SESSION['login']; define('DB_NAME', ''); define('DB_USER', ''); define('DB_PASSWORD', ''); define('DB_HOST', ''); $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' .mysqli_error()); } $db_selected = mysqli_select_db( $link, DB_NAME); if (!$db_selected) { die('Could not connect: ' .mysqli_connect_error()); } $username = $_POST['username']; $password = $_POST['password']; $username = mysqli_real_escape_string($link,$username); $password = mysqli_real_escape_string($link,$password); $sql = "SELECT * FROM mainLogin WHERE username = '$username'"; $result = mysqli_query($link, $sql); $count=mysqli_num_rows($result); if($count==1){ $row = mysqli_fetch_assoc($result); if ($password == $row['password']){ //IMPORTANT PART //IMPORTANT PART //IMPORTANT PART //IMPORTANT PART $_SESSION["login"] = $username; echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com/ptable/pTable.html'); </script>"; return true; } else { echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com'); </script>"; return false; } } else{ echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com'); </script>"; return false; } mysqli_close($link); ?>我在我的html文件中检查会话变量,如下所示:
<!DOCTYPE html> <?php session_start(); if (!isset($_SESSION['login'])) { header('HTTP/1.1 403 Forbidden'); exit(); } ?> <html> <meta name="viewport" content="width=device-width, initial-scale=1"> <link id="pagestyle" rel="stylesheet" type="text/css" href="pTableStylesheet.css"> <title>Periodic Table</title> <head> <script src="script.js" type=text/javascript></script> <p id=welcome>Welcome to our Periodic Table Of Elements.<br> ... ... ... ...当我从我没有登录的浏览器转到ptable.html页面时,我应该被重定向到错误页面吗?
这不起作用。 谁能明白为什么?
谢谢。
So i have a login.php that starts a sessions and sets 'login', then redirects the user to a html page (pTable.html) like this:
<?ph //starting session session_start(); //connecting to database and such echo $_SESSION['login']; define('DB_NAME', ''); define('DB_USER', ''); define('DB_PASSWORD', ''); define('DB_HOST', ''); $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' .mysqli_error()); } $db_selected = mysqli_select_db( $link, DB_NAME); if (!$db_selected) { die('Could not connect: ' .mysqli_connect_error()); } $username = $_POST['username']; $password = $_POST['password']; $username = mysqli_real_escape_string($link,$username); $password = mysqli_real_escape_string($link,$password); $sql = "SELECT * FROM mainLogin WHERE username = '$username'"; $result = mysqli_query($link, $sql); $count=mysqli_num_rows($result); if($count==1){ $row = mysqli_fetch_assoc($result); if ($password == $row['password']){ //IMPORTANT PART //IMPORTANT PART //IMPORTANT PART //IMPORTANT PART $_SESSION["login"] = $username; echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com/ptable/pTable.html'); </script>"; return true; } else { echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com'); </script>"; return false; } } else{ echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com'); </script>"; return false; } mysqli_close($link); ?>And i am cheking for the session variable in my html file like this:
<!DOCTYPE html> <?php session_start(); if (!isset($_SESSION['login'])) { header('HTTP/1.1 403 Forbidden'); exit(); } ?> <html> <meta name="viewport" content="width=device-width, initial-scale=1"> <link id="pagestyle" rel="stylesheet" type="text/css" href="pTableStylesheet.css"> <title>Periodic Table</title> <head> <script src="script.js" type=text/javascript></script> <p id=welcome>Welcome to our Periodic Table Of Elements.<br> ... ... ... ...when i go to the ptable.html page from a browser that i have not logged in to, i should be redirected to the error page right?
This is not working. Can anyone see why?
Thanks.
最满意答案
将文件名从ptable.html更改为ptable.php以运行php代码。
并使用下面的代码重定向到login.php如果没有登录,如果尝试访问ptable页面。
<?php session_start(); if (!isset($_SESSION['login'])) { header("Location: login.php"); exit(); }还可以使用ptable.html到ptable.php更改链接或操作
Change your file name from ptable.html to ptable.php to run php code.
and use below code to redirect to login.php if not logged in and if try to access ptable page.
<?php session_start(); if (!isset($_SESSION['login'])) { header("Location: login.php"); exit(); }Also change link or action where ever you used ptable.html to ptable.php
检查登录html页面(Checking for login on html page)所以我有一个login.php来启动会话并设置'login',然后将用户重定向到一个html页面(pTable.html),如下所示:
<?ph //starting session session_start(); //connecting to database and such echo $_SESSION['login']; define('DB_NAME', ''); define('DB_USER', ''); define('DB_PASSWORD', ''); define('DB_HOST', ''); $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' .mysqli_error()); } $db_selected = mysqli_select_db( $link, DB_NAME); if (!$db_selected) { die('Could not connect: ' .mysqli_connect_error()); } $username = $_POST['username']; $password = $_POST['password']; $username = mysqli_real_escape_string($link,$username); $password = mysqli_real_escape_string($link,$password); $sql = "SELECT * FROM mainLogin WHERE username = '$username'"; $result = mysqli_query($link, $sql); $count=mysqli_num_rows($result); if($count==1){ $row = mysqli_fetch_assoc($result); if ($password == $row['password']){ //IMPORTANT PART //IMPORTANT PART //IMPORTANT PART //IMPORTANT PART $_SESSION["login"] = $username; echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com/ptable/pTable.html'); </script>"; return true; } else { echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com'); </script>"; return false; } } else{ echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com'); </script>"; return false; } mysqli_close($link); ?>我在我的html文件中检查会话变量,如下所示:
<!DOCTYPE html> <?php session_start(); if (!isset($_SESSION['login'])) { header('HTTP/1.1 403 Forbidden'); exit(); } ?> <html> <meta name="viewport" content="width=device-width, initial-scale=1"> <link id="pagestyle" rel="stylesheet" type="text/css" href="pTableStylesheet.css"> <title>Periodic Table</title> <head> <script src="script.js" type=text/javascript></script> <p id=welcome>Welcome to our Periodic Table Of Elements.<br> ... ... ... ...当我从我没有登录的浏览器转到ptable.html页面时,我应该被重定向到错误页面吗?
这不起作用。 谁能明白为什么?
谢谢。
So i have a login.php that starts a sessions and sets 'login', then redirects the user to a html page (pTable.html) like this:
<?ph //starting session session_start(); //connecting to database and such echo $_SESSION['login']; define('DB_NAME', ''); define('DB_USER', ''); define('DB_PASSWORD', ''); define('DB_HOST', ''); $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' .mysqli_error()); } $db_selected = mysqli_select_db( $link, DB_NAME); if (!$db_selected) { die('Could not connect: ' .mysqli_connect_error()); } $username = $_POST['username']; $password = $_POST['password']; $username = mysqli_real_escape_string($link,$username); $password = mysqli_real_escape_string($link,$password); $sql = "SELECT * FROM mainLogin WHERE username = '$username'"; $result = mysqli_query($link, $sql); $count=mysqli_num_rows($result); if($count==1){ $row = mysqli_fetch_assoc($result); if ($password == $row['password']){ //IMPORTANT PART //IMPORTANT PART //IMPORTANT PART //IMPORTANT PART $_SESSION["login"] = $username; echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com/ptable/pTable.html'); </script>"; return true; } else { echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com'); </script>"; return false; } } else{ echo "<script> window.location.assign('http://madsanker.dk.linux101.unoeuro-server.com'); </script>"; return false; } mysqli_close($link); ?>And i am cheking for the session variable in my html file like this:
<!DOCTYPE html> <?php session_start(); if (!isset($_SESSION['login'])) { header('HTTP/1.1 403 Forbidden'); exit(); } ?> <html> <meta name="viewport" content="width=device-width, initial-scale=1"> <link id="pagestyle" rel="stylesheet" type="text/css" href="pTableStylesheet.css"> <title>Periodic Table</title> <head> <script src="script.js" type=text/javascript></script> <p id=welcome>Welcome to our Periodic Table Of Elements.<br> ... ... ... ...when i go to the ptable.html page from a browser that i have not logged in to, i should be redirected to the error page right?
This is not working. Can anyone see why?
Thanks.
最满意答案
将文件名从ptable.html更改为ptable.php以运行php代码。
并使用下面的代码重定向到login.php如果没有登录,如果尝试访问ptable页面。
<?php session_start(); if (!isset($_SESSION['login'])) { header("Location: login.php"); exit(); }还可以使用ptable.html到ptable.php更改链接或操作
Change your file name from ptable.html to ptable.php to run php code.
and use below code to redirect to login.php if not logged in and if try to access ptable page.
<?php session_start(); if (!isset($_SESSION['login'])) { header("Location: login.php"); exit(); }Also change link or action where ever you used ptable.html to ptable.php
发布评论