Note that there are some explanatory texts on larger screens.

plurals
  1. POQuestions about User Authorization in PHP
    primarykey
    data
    text
    <p>I'm trying to build a simple user authorization system where a .txt file stores the username and password information. I know this probably should be done differently (via databases), but I'm doing this for practice. If anyone could help me with my questions, it would be greatly appreciated. </p> <p>So far I have the register.php and login.php built, but I'm not sure where to go from here. Here are my three questions:</p> <ol> <li><p>Let's say I have an html site that I want protected. How would I incorporate the login.php so if a user who is not logged in tries to access the content of the pages, they are prompted to log in? Would I have to put something in the of each html page?</p></li> <li><p>How would I implement cookies in this process? A user who logged in shouldn't have to re-login for 20 minutes. Ideally, I would want to send the cookies after the user has logged in, but how would I do this?</p></li> <li><p>I want my register.php to check for existing users so all names are unique. What I have so far does not do this correctly. What is wrong with my code?</p></li> </ol> <p>Here is my code:</p> <p>LOGIN.PHP</p> <pre><code>&lt;?php $check = 0; if (isset($_POST['submit'])) { $name = htmlentities($_POST['name']); $name = strtolower($name); $password = htmlentities($_POST['apw']); $filename = getcwd() . "/psverification.txt"; $lines = file( $filename , FILE_IGNORE_NEW_LINES ); printf ("Hi %s,&lt;/br /&gt;", $name); foreach($lines as $key =&gt; $line) { list($username, $pw) = explode('|', $line); if($username == $name &amp;&amp; $pw == $password) $check++; break; } if ($check == 1){ //Redirect to home page Header("Location: index.html"); } else{ printf("Your username or password are invalid. Please try again."); } } ?&gt; &lt;form method = "POST" action = "&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; &lt;p&gt; Username:&lt;br /&gt; &lt;input type = "text" id="name" name="name" size="20" maxlength="40" /&gt; &lt;/p&gt; &lt;p&gt; E-mail Address:&lt;br /&gt; &lt;input type = "text" id="apw" name="apw" size="20" maxlength="40" /&gt; &lt;/p&gt; &lt;input type="submit" id="submit" name ="submit" name ="submit" value="Log in" /&gt; &lt;p&gt; &lt;a href="register.php"&gt;Register&lt;/a&gt;&lt;/p&gt; &lt;/form&gt; </code></pre> <p>REGISTER.PHP</p> <pre><code>&lt;?php if (isset($_POST['submit'])) { $username = $_POST['user']; $password = $_POST['password']; $confirmpw = $_POST['confirmpw']; $username = strtolower($username); //Check if passwords match if ($password != $confirmpw){ print "Passwords do not match, please try again."; } else{ //the data $data = "$username|$password\n"; //open the file and choose the mode $fh = fopen("psverification.txt", "a+"); // Cycle through the array while (($buffer = fgets($fh, 4096)) !== false) { // Parse the line list($usercheck, $passwordcheck) = explode('|', $buffer); if (trim($usercheck) == $username) { print "The username is already in our system. Please use another one.";} else { fwrite($fh, $data); //Redirect to home page Header("Location: index.html"); } } //close the file fclose($fh); } } ?&gt; &lt;form method = "POST" action = "&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; &lt;p&gt; Username:&lt;br /&gt; &lt;input type = "text" id="user" name="user" size="20" maxlength="40" /&gt; &lt;/p&gt; &lt;p&gt; Password:&lt;br /&gt; &lt;input type = "password" id="password" name="password" size="20" maxlength="40" /&gt; &lt;/p&gt; Confirm Password:&lt;br /&gt; &lt;input type = "password" id="confirmpw" name="confirmpw" size="20" maxlength="40" /&gt; &lt;/p&gt; &lt;input type="submit" id="submit" name ="submit" name ="submit" value="Register" /&gt; &lt;/form&gt; </code></pre> <p>Thanks in advance. Any examples would be greatly appreciated. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload