Php 如何在没有sql的情况下添加登录/注销功能和注册
我正在学习PHP,我主要关心的是在我的网站上添加活动,但我不懂SQL。没有SQL有没有办法做到这一点?您可能可以使用平面文件-唯一的问题可能是并行添加新帐户。但是,如果您想在网站开发中更深入地挖掘,SQL绝对是一个不错的选择。对于一个快速而肮脏的解决方案,您可以将用户凭据存储在一个数组中,例如:Php 如何在没有sql的情况下添加登录/注销功能和注册,php,Php,我正在学习PHP,我主要关心的是在我的网站上添加活动,但我不懂SQL。没有SQL有没有办法做到这一点?您可能可以使用平面文件-唯一的问题可能是并行添加新帐户。但是,如果您想在网站开发中更深入地挖掘,SQL绝对是一个不错的选择。对于一个快速而肮脏的解决方案,您可以将用户凭据存储在一个数组中,例如: $creds = array( array( 'username' => 'john123', 'password' =
$creds = array(
array( 'username' => 'john123',
'password' => 'hello'),
array( 'username' => 'lucyliu',
'password' => 'dieblackmamba')
//more sub arrays like above here
);
然后,您可以匹配用户的输入,例如:
$username = $_POST['username'];
$password = $_POST['password'];
$match = false;
for($i=0;$i<count($creds);$i++) {
if(strcmp($username,$creds[$i]['username']) == 0) {
if(strcmp($password,$creds[$i]['password']) == 0) {
// start session and set something to indicate that user is logged in
session_start();
$_SESSION['authenticated'] = true;
$_SESSION['username'] = $creds[$i]['username'];
$match = true;
}
}
}
if($match) echo 'Login successful: welcome ' . $creds[$i]['username'];
else echo 'Invalid credentials';
要注销用户,可以执行以下代码:
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// Finally, destroy the session.
session_destroy();
阅读有关php和的内容会对您有所帮助,如果我是您,我会咬紧牙关,尝试使用SQL。检查会话处理的工作方式,这样您就可以保存您的用户对象
仅保存会话在单服务器环境中有效,在多服务器环境中,您需要将会话保存到数据库等公共位置,但对于更简单的页面和学习,仅保存到会话有效您需要了解的SQL非常基本,非常值得学习。看看这些结果中的任何一个:
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// Finally, destroy the session.
session_destroy();