0

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>
Daedalus
  • 7,586
  • 5
  • 36
  • 61
user1656125
  • 107
  • 1
  • 8

2 Answers2

0

You are correct that double quotes parse variables, while single quotes do not. As to your second question in that paragraph, see here for rules regarding wrapping attribute values in quotes. There isn't really all that much benefit in doing so aside from being able to have special characters within the value. I would also recommend reading this.

As to your third question in that paragraph(the mini-question), I would recommend the following:

echo "<a href='$com.php'>".usrName($x)."</a>";

If you want to just use the function directly in the line.

As to your very first question, I do not know of any such way to include a file without passing the html within. This might be such an answer, but it's untested on my end, so I couldn't tell you.

Community
  • 1
  • 1
Daedalus
  • 7,586
  • 5
  • 36
  • 61
  • If this answer is satisfactory, please click the outlined checkmark under the down arrow to accept it. – Daedalus Sep 15 '12 at 02:11
0

If you are looking to send the users name to the next php file, you can pass the variable on like this:

echo "<a href='$com.php?usrName=$usrNameLink&someOtherVariable=2'>$usrNameLink</a> ";

And

else header('Location: com.php?usrName=$usrNameLink&someOtherVariable=2'); 

Then on the next php page use $_GET['usrName'] to retrieve it just like you are for the POST variables

Other than that Daedalus has covered all the rest!

Hank
  • 731
  • 6
  • 10