Help with a "remember me" checkbox in PHP

Help with a "remember me" checkbox in PHP
0

#1

Got an Auto Indexer open source author Justin Hagstrom

Needed to add an “remember me” function. Did a tutorial but I"m getting an error at first I couldn’t see my page turns out it was a missing bracket now I’m getting a broken code. The login works but it’s breaking in the remember me checkbox.

Here’s the website link http://www.mesc.org/gatedTEST/index.php

Here’s my code in the index.php:

$log_login = false;

if (USE_LOGIN_SYSTEM && isset($_POST['username'], $_POST['password']) && $_POST['username'] != '' && $_POST['password'] != '')
  {
  $you = new UserLoggedIn($_POST['username'], sha1($_POST['password']));
  $log_login = true;
  $_SESSION['password'] = sha1($_POST['password']);
  unset($_POST['password']);
  $_SESSION['username'] = $_POST['username'];
  }
  else
if (USE_LOGIN_SYSTEM && isset($_SESSION['username'], $_SESSION['password']))
  {
  $you = new UserLoggedIn($_SESSION['username'], $_SESSION['password']);
  }
  else
if (USE_LOGIN_SYSTEM && isset($_COOKIE['username'], $_COOKIE['password']))
  {
  $you = new UserLoggedIn($_COOKIE['username'], $_COOKIE['password']);
  }
  else
  {
  $you = new User();
  if (MUST_LOGIN_TO_DOWNLOAD && USE_LOGIN_SYSTEM)
    {
    $str = '<p>You must login to view and download files. </p>' . '<table border="0" cellpadding="8" cellspacing="0">' . '<tr class="paragraph"><td class="autoindex_td">' . $you->login_box() . '</td></tr></table>';
    echo new Display($str);
    die();
    }
  }

if (!empty($_POST["remember"]))
  {
  setcookie("user", $_POST["username"], time() + (10 * 365 * 24 * 60 * 60));
  setcookie("pass", $_POST["password"], time() + (10 * 365 * 24 * 60 * 60));
  }
  else
  {
  if (isset($_COOKIE["user"]))
    {
    setcookie("user", "");
    }

  if (isset($_COOKIE["pass"]))
    {
    setcookie("pass", "");
    }
  }

Here what is in the user.php

public function login_box() {
  $str = '';
  if (USE_LOGIN_SYSTEM) {
    global $words, $subdir;
    $str .= '<div itemprop="articleBody">
            <div class="bootstrapCSS">
			<div class="in-container service-container">
			<div class="bidders-container">
			<h2 style="text-align:center">Please Login</h2>
			<form action="' /*. Url::html_output($_SERVER['PHP_SELF']) .*/ . $_SERVER['REQUEST_URI'] /*'?dir='*/ . (isset($subdir) ? rawurlencode($subdir) : '') . '" method="post">
			<div class="col-md-6 col-md-offset-3">
			<div class="form-group">
			<label for="login_user" class="form-label">' . $words -> __get('username') . ':</label>
			<input type="text" name="username" class="form-control" placeholder="' . $words -> __get('username') . '" value="<?php if(isset($_COOKIE["user"])) {echo $_COOKIE["user"];} ?>"/>'. '
			</div>
			<div class="form-group">
			<label for="login_pass" class="form-label">' . $words -> __get('password') . ':</label>
			<input type="password" name="password" placeholder="****" class="form-control" value="<?php if(isset($_COOKIE["pass"])) {echo $_COOKIE["pass"];}?>"/>	
			<input type="checkbox" name="remember" <?php if(isset($_COOKIE["user"])) { ?> checked <?php }?> /> 
			<label for="remember-me">Remember Me</label>
			</div>
			<input type="submit" name="Submit" value="' . $words -> __get('login') . '" class="button2 btn btn-primary btn-flat">
			<div class="clearfix"></div>
			</div>
			</form>
			</div></div></div></div>'
			;
		}

		
		
		if (LEVEL_TO_UPLOAD === GUEST)
		{
			global $you;
			$upload_panel = new Upload($you);
			$str .= $upload_panel -> __toString();
		}
		return $str;
	}

thank you for your help and time.


#2

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://freecodecamp.org.

Also, I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.