gpt4 book ai didi

php - 使用 session_start() 限制对页面的访问

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:01 24 4
gpt4 key购买 nike

我在一个登录页面上工作,如果你登录,你会被重定向到上传页面,如果你没有登录,我试图限制对上传页面的访问,我不希望人们有如果他们没有登录,请访问页面。到目前为止,这是我的代码,但我不知道如何限制 session 的访问。我的登录脚本:

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="ana"; // Database name
$tbl_name="user"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['user'];
$mypassword=$_POST['pass'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE user='$myusername' and pass='$mypassword'";
$result=mysql_query($sql);


// If result matched $myusername and $mypassword, table row must be 1 row
if($myusername=='ana' and $mypassword==''){
session_start();
$_SESSION["myusername"]=$myusername;
$_SESSION["mypassword"]=$mypassword;
// Register $myusername, $mypassword and redirect to file "login_success.php"
echo "Your login was succesfull!";
header("refresh:3;url=upload.php");
}
else {
echo "Wrong Username or Password, please try again.";
header("refresh:3;url=connect.php");
}

?>

将您重定向到它的页面是:

<!DOCTYPE HTML>
<!--
Astral by HTML5 UP
html5up.net | @n33co
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<?php
session_start();
?>
<html>
<head>
<title>Ana Gemescu - Work work work | Upload </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<!--[if lte IE 8]><script src="css/ie/html5shiv.js"></script><![endif]-->
<script src="js/jquery.min.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/init.js"></script>
<noscript>
<link rel="stylesheet" href="css/skel.css" />
</noscript>
<!--[if lte IE 8]><link rel="stylesheet" href="css/ie/v8.css" /><![endif]-->
</head>
<body>
<!-- Wrapper-->
<div id="wrapper">

<!-- Main -->
<div id="main">
<!-- Me -->
<article id="me" class="panel">
<header>
<form action="uploader.php" method="post" enctype="multipart/form-data">
Select image to upload:<br />
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<select name="Folder" style="width:500px;margin-bottom:5px;margin-top:5px;" >
<option value="photo" style="padding:2px">Photos</option>
<option value="draw" style="padding:2px">Drawings</option>
<option value="video" style="padding:2px">Videos</option>
<option value="other" style="padding:2px">Other</option>
</select><br />
<input type="submit" value="Upload Image" name="submit">
</form>
</header>
</article>
</div>
<!-- Footer -->
<div id="footer">
<ul class="copyright">
<li>&copy; Ana Gemescu</li><li>Design: <a href="http://html5up.net">HTML5 UP</a>, Coded by: <a href="#">zapo</a></li>
</ul>
</div>

</div>

</body>
</html>

请有人帮助我,如果您未登录,我如何限制对上传页面的访问?

如果您需要更多信息,请告诉我

最佳答案

检查 $_SESSION["myusername"],像这样

if(ISSET($_SESSION["myusername"]))
{
//upload page code
}
else
{
print "access denied";
}

关于php - 使用 session_start() 限制对页面的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28071347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com