我需要使用PHP登录脚本并编写该脚本(请参见下面的脚本),但是表单没有通过URL(使用GET或POST)将输入文件中的变量发送到同一文件中
<?php
session_start();
$admin=true;
function controle($uname, $pword, $admin){
$datatable = "my_table";
$servername = 'localhost';
$username = 'admin';
$password = '1234';
$database = 'myDB';
//Create connection
$con = mysqli_connect($servername, $username,
$password, $database);
if ($con == false){
die("STATUS: Error: ".mysqli_error_connect());
}
if ($admin == true){
$sql = "SELECT * FROM ".$datatable;
}
$result = mysqli_query($con,$sql);
$rows=mysqli_fetch_assoc($result);
$sql2 = "SELECT COUNT(*) AS total FROM ".$datatable;
$result = mysqli_query($con,$sql2);
$row2 = $result->fetch_row();
$total_records = $row2[0];
if ($total_records > 0){
$gebruikersnaam = $rows['username'];
$wachtwoord = $rows['password'];
if ($pword != $wachtwoord){
return false;
} else {
return $gebruikersnaam;
}
} else {
return false;
}
}
// Check name and password
if (isset($_POST['verzonden'])){
$username = $_POST['username'];
$password = md5($_POST['wachtwoord']);
$login_ok = controle($username, $password, $admin);
if (login_ok != false){
//correct
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
}
}
if (controle($_SESSION['username'],
$_SESSION['wachtwoord'], $admin) == false) {
echo "<form method='post'
action='".$_SERVER['PHP_SELF']."?";
reset($_GET); // put the array pointer to 0 when
starting
// Send the variables again
while($getvar = each($_GET)){
$varName = $getvar['key'];
$varValue = $getvar['value'];
echo "$varName=$varValue&";
}
echo "'><br><br>";
echo "Name: ";
echo "<input type='text' name='username'>";
echo "<br>";
echo "Password: ";
echo "<input type='text' name='wachtwoord'>";
echo "<br>";
echo "<input type='submit' value='log in'
name='verzonden'>";
echo "</form>";
if ($admin == "true"){
echo "<p>-- ADMIN status is vereist!";
}
exit;
}
?>
但是该表格似乎没有发送任何内容。.我期待的是:
authentication.php?username=MYNAME&wachtwoord=4321
与数据库的连接正常,我可以从那里读取变量。我没有ID,为什么我的表单不发送仅获得“ authentication.php”的变量?
答案 0 :(得分:2)
您正在混淆$ _GET和$ _POST。 您的表单正在使用$ _POST,但是您正在寻找$ _GET变量。
我已更改您的代码,以使其正常工作。如果您想调试并查看整个过程中发生的事情,我留了一些var_dump
行
我已将您的数据库连接设置为全局连接,以便您仅连接一次 ,并且您可以在需要时再次访问它。 我已将您的GET更改为POST。当您登录或何时需要登录时,我已经显示了它。 我也为您建立了一个注销部分。
<?php
session_start();
$admin=true;
$datatable = "my_table";
$servername = 'localhost';
$username = 'nick';
$password = 'nickd18';
$database = 'test';
// show session variables for debuggin;
var_dump($_SESSION);
// you may want to destroy session variables at some point when debugging (should be a log out but we can use this line for now)
//
if (isset($_GET) && $_GET['logout'] == '1') {
session_destroy();
$login = "http://".$_SERVER[HTTP_HOST].$_SERVER['PHP_SELF'];
die(header('Location: '.$login));
}
//Create connection
global $con;
$con = mysqli_connect($servername, $username, $password, $database);
if ($con == false){
die("STATUS: Error: ".mysqli_error_connect());
}
function check_login($uname, $pword, $admin){
global $con;
$sql = "SELECT * FROM `my_table` WHERE `username` = '$uname' AND `password` = '$pword' LIMIT 1";
//var_dump($sql);
$result = mysqli_query($con,$sql);
//var_dump($result);
$rows=mysqli_fetch_assoc($result);
if ($rows) {
/* successful login */
return true;
} else {
/* failed login */
return false;
}
}
// Check name and password
if (isset($_POST['verzonden'])){
//var_dump($_POST);
$username = $_POST['username'];
$password = md5($_POST['wachtwoord']);
$login_ok = check_login($username, $password, $admin);
if ($login_ok != false){
//correct
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
}
}
if (!$_SESSION['username']) {
echo "<h2>You are not logged in</h2>";
echo "<form method='post' action='".$_SERVER['PHP_SELF']."?";
echo "'><br><br>";
echo "Name: ";
echo "<input type='text' name='username'>";
echo "<br>";
echo "Password: ";
echo "<input type='text' name='wachtwoord'>";
echo "<br>";
echo "<input type='submit' value='log in'
name='verzonden'>";
echo "</form>";
if ($admin == "true"){
echo "<p>-- ADMIN status is vereist!";
}
exit;
} else {
echo "<h1>You are logged in as ".$_SESSION['username']."</h1>";
echo "<p><a href='?logout=1'>Log out</a>";
}
?>