我正在尝试在我的网站 (index.php) 上进行登录:
我制作了一个表格并将信息发送到validation-sign-in.php文件以进行验证:
(我稍后会添加加密)
这是表格:
<div class="sign-in-up">
<div id="sign-in-modal" class="modal">
<form method="post" class="modal-content animate" action="php/validation-sign-in.php">
<div class="imgcontainer">
<span onclick="document.getElementById('sign-in-modal').style.display='none'" class="close" title="Close Modal">×</span>
<img src="images/avatar.png" alt="Avatar" class="avatar" style="width:100px;height:100px">
</div>
<div class="container-sign-in-up">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="UserName" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="Password" required>
<button type="submit">Sign In</button>
<input
type="button"
id="sign-up-btn"
onclick="document.getElementById('sign-up-modal').style.display='block';
document.getElementById('sign-in-modal').style.display='none'"
value="Sign Up">
<input type="checkbox" checked="checked"> Remember me
</div>
<div class="container-sign-in-up" style="background-color:#f1f1f1">
<button type="button" onclick="document.getElementById('sign-in-modal').style.display='none'" class="cancelbtn">Cancel</button>
<span class="psw">Forgot <a href="#">password?</a></span>
</div>
</form>
</div>
</div>
这是validation-sign-in.php:
<?php
if(empty($_POST)){
header('location:../index.php');
}
$con=mysqli_connect("localhost","root","","typing_club");
$user = $_POST['UserName'];
$pass = $_POST['Password'];
$query = "SELECT * FROM Users WHERE " . "UserName = '".$user."' AND Password='". $pass."'" ;
echo "$query";
$result = mysqli_query($con,$query);
if (mysqli_num_rows($result) == 1) {
echo "query successfull wrote to DB";
header('location:../index.php?validation=true');
} else {
echo"unscccessful login";
header('location:../index.php?validation=false');
}
?>
每件事都很好。
但我需要获取登录用户的用户名和 ID,这是为了跟踪用户,更新他的进度,并在网上写下他的名字......
我不能比 John Conde 更强调你不遵循正确的标准给用户和你自己带来的安全隐患。请在进一步进行之前修复您的代码。一定要阅读 OWASP 关于 PHP 的部分,可以在这里找到。
您错误地认为,如果$_POST
填充了所有必填字段,则该假设是不正确的,并且会由于未定义的变量错误而导致您的页面中断。确保在继续访问它们之前设置所有字段。
echo "$query";
你还在这里做什么!?那应该去掉!
echo "query successfull wrote to DB";
你从来没有向数据库写过任何东西!您只是确保凭据正确。
包含当前脚本可用的会话变量的关联数组。有关如何使用它的更多信息,请参阅会话函数文档。
第1页
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';
// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
第2页
<?php
// page2.php
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句