我正在创建一个简单的消息传递应用程序,用户可以在其中登录或注册并专门与我交流。他们的用户名和密码存储在 .txt 中的一行(存储其他所有人),我们的对话逐行存储在唯一的用户名.txt 中
当用户注册或登录时,我想将他们从表单重定向到一个独立的文本区域,该区域在下面显示我们的对话。但是,当重定向用户时,我不确定如何将他们的用户名 (usrNm) 发送到下一个 php 文件以显示我们对话的内容(该文件是他们的用户名)。我试过 include(index.php) 无济于事。如何在不传递 html 的情况下也包含 index.php。最好的方法是什么?
我也对使用''
vs的一般规则感到困惑""
,我相信""
允许变量在里面并''
适用于其他一切?我还在 html 中被告知<input type="text"
.. 文本应该用引号括起来,如果它不带引号工作有什么好处?如果你想启发我,另一个小问题在第 30 行。非常感谢所有建议!
//index.php
<?php
echo "Login or register. ";
//Ensures form exists and instantiates neccesary variables
if (isset($_POST['usrNm'],$_POST['pass'],$_POST['passConf'])) {
$usrNm = $_POST['usrNm'];
$pass = $_POST['pass'];
$passConf = $_POST['passConf'];
$rec = file('userPass.txt');
//Parses username from users txt file
function usrName($indx){
global $rec;
$usrName = substr($rec[$indx], 0, stripos($rec[$indx], ' '));
return $usrName;
}
//Parses password from users txt file
function passWrd($indx){
global $rec;
$passWrd = trim(substr($rec[$indx], stripos($rec[$indx], ' ')));
return $passWrd;
}
//Attempts to log user in
if(!empty($usrNm)&&!empty($pass)&&empty($passConf)) {
for($i=0;$i<count($rec);$i++) {
//Logs user in
if (($usrNm==usrName($i))&&($pass==passWrd($i))) {
//Outputs admin screen
if ($usrNm=='rich') {
for ($x=1; $x<count($rec); $x++) {
//How to evaluate usrName($x) between link tags?
$usrNameLink = usrName($x);
echo "<a href='$com.php'>$usrNameLink</a> ";
}
break;
}
else header('Location: com.php');
}
//Wrong password
elseif (($usrNm==usrName($i))&&($pass!=passWrd($i))) {
echo 'Wrong password.';
break;
}
//Username doesn't exist
elseif (($i==count($rec)-1)&&($usrNm!=usrName($i))) {
echo 'Username doesn\'t exist.';
}
}
}
//Attempts to register user.
elseif (!empty($usrNm)&&!empty($pass)&&!empty($passConf)) {
for($i=0;$i<count($rec);$i++) {
//Username taken
if ($usrNm==usrName($i)) {
echo 'Username taken.';
break;
}
//Registers user: updates user txt file and creates txt file that saves correspondence between the user and me
elseif (($i==count($rec)-1)&&($usrNm!=usrName($i))&&($pass==$passConf)) {
$handle = fopen('userPass.txt', 'a');
$handle2 = fopen("$usrNm.txt", 'a');
fwrite($handle, $usrNm.' '.$pass."\r\n");
fwrite($handle2, 'Richard: Here you can send me messages.');
fclose($handle);
fclose($handle2);
header('Location: com.php');
}
//Passwords don't match
elseif (($i==count($rec)-1)&&($usrNm!=usrName($i))&&($pass!=$passConf)) {
echo 'Passwords don\'t match.';
}
}
}
//User didn't fill in the password field
else{echo 'Fill in the required fields.';}
}
?>
<form action="index.php" method="post">
Username:<input name="usrNm" type="text"></input><br>
Password:<input name="pass" type="password"></input><br>
Confirm Password (only for registration):<input name="passConf" type="password"></input><br>
<input name="submit" type="submit"></input>
</form>
//com.php
<?php
include('index.php');
if (isset($_POST['msg'])) {
if (!empty($_POST['msg'])) {
echo $usrNm;
}
}
?>
Welcome!
<form action="com.php" method="post">
<textarea name="msg" cols="64" rows="4" type="text"></textarea><br>
<input name="send" type="submit"></input>
</form>