我尝试过添加PHP会话,因此他们不必一直填写相同的密码。如果我转到页面并填写代码,它将显示具有正确会话的页面,但是当我刷新页面时,会话就消失了。
会话代码:
<head>
<script>
{ background-color:#87CEFA; }
</script>
</head>
<?php
error_reporting(0);
session_start();
$_SESSION["pass"] = $_POST["code"];
$pass = $_POST["code"];
$con=mysqli_connect("localhost","bb","$pass","bb");
if (mysqli_connect_errno($con))
{
echo "Kan geen verbinding maken, de ingevulde code is verkeerd of de server is offline!";
echo 'Hello '.$_SESSION["pass"].'!';
}
$result = mysqli_query($con,"SELECT * FROM ftp");
while($row = mysqli_fetch_array($result))
{
$myuser = $row['user'];
$mypass = $row['pass'];
$myhost = $row['host'];
$conn_id = ftp_connect($myhost) or die("Couldn't connect to $myhost");
if (@ftp_login($conn_id, $myuser, $mypass))
{
//Path to your *.txt file:
$file = $ftp_server['DOCUMENT_ROOT'] . "bbbb";
$contents = file($file);
$string = implode($contents);
echo $string;
}
}
mysqli_close($con);
?>
感谢。
答案 0 :(得分:0)
在每次加载页面时,您都运行代码$_SESSION["pass"] = $_POST["code"];
。然后你试着像echo 'Hello '.$_SESSION["pass"].'!';
那样回应它。你基本上做的是回应$_POST["code"]
。只有在您提交表单时才会设置$_POST
变量。
答案 1 :(得分:0)
您在每次页面加载时都会覆盖会话变量。您需要在设置会话变量之前检查表单是否已提交:
session_start();
$_SESSION["pass"] = $_POST["code"];
需要
session_start();
if ('POST' === $_SERVER['REQUEST_METHOD']) {
$_SESSION["pass"] = $_POST["code"];
}
答案 2 :(得分:0)
答案 3 :(得分:0)
我很遗憾地说这个,但你真的需要阅读更多关于 php session 的内容。
我采取了一些自由重写和评论,但如果你有任何不明白的地方,请询问
<?php
// protect against any output, leave no spaces or shenanigans before the session_start()
// even warnings and errors
// this is the first thing that has to happen BEFORE ANY OUTPUT
session_start();
// is this a good idea when you're in development
error_reporting(0);
// we don't know if $_POST['whateva'] actually exists
if (isset($_POST['code'])) {
// this may not be such a good idea...
$_SESSION['pass'] = $_POST['code'];
}
$pass = isset($_POST['code']) ? $_POST['code'] : '';
$error = '';
try {
$con = mysqli_connect('localhost','bb',$pass,'bb');// is this barebones MVC? just curious
if (mysqli_connect_errno($con)) throw new Exception('Kan geen verbinding maken, de ingevulde code is verkeerd of de server is offline! Hello '.$_SESSION['pass'].'!');
$result = mysqli_query($con,'SELECT * FROM ftp');
$ftpContent = array();
while ($row = mysqli_fetch_array($result)) {
$myuser = $row['user'];
$mypass = $row['pass'];
$myhost = $row['host'];
if (!$conn_id = ftp_connect($myhost)) throw new Exception('Couldn\'t connect to '.$myhost);
if (@ftp_login($conn_id,$myuser,$mypass)) {
//Path to your *.txt file:
$file = $ftp_server['DOCUMENT_ROOT'].'bbbb';// where does this $ftp_server come from????
$contents = file($file);
$string = implode($contents);
$ftpContent[$myuser] = $string;
}
}
mysqli_close($con);
} catch (Exception $e) {
$error = $e->getMessage();
}
// now output all your stuff here
?>
<!DOCTYPE html>
<html>
<head>
<style>body{background-color:#87cefa;}.error{color:#f00;}</style>
</head>
<body>
<?php if ($error) echo '<p class="error">There has been an error: '.$error.'</p>'; ?>
</body>
</html>
不要复制粘贴,查看已完成的内容并评估会发生什么,还有一个缺失的变量,所以你必须要处理