I'm creating a simple messaging application where users can login or register and communicate exclusively with me. Their user name and password is stored on a line in a .txt (that stores everyone elses) and our conversation is stored line by line in a unique user'sname.txt
When the user register or logs in I want to redirect them away from the form and to a standalone text area that displays our conversation below. However, when redirecting the user I'm not sure how to send their user name (usrNm) to the next php file so as to display the contents of our conversation (the file is their username). I've tried include(index.php) to no avail. How do also include index.php without passing the html. What's the best way to do this?
I'm also confused about the general rules of using ''
vs ""
, I believe ""
allows variables to be inside and ''
works for everything else? I was also told in html <input type="text"
.. the text should be in quotes, what are the benefits if it works without quotes? Another mini question is on line 30 if you wanna enlighten me. All advice is GREATLY appreciated!
//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>