0

I am following a tutorial and doing exactly everything the same. I'm not sure if I just have a typo somewhere or what, but at this point I am completely lost. I made a form and it is giving me multiple errors when I try to put a value field in to remember the value I typed in. Here is the page source since the console is not showing errors. I will provide will all the pages required to help.

ANSWER EDIT: I have found out the problem for all the errors. The first error I get is Undefined index: username in .... So to fix that in the input.php class I made. for the Input::get() function where it says if(isset($_POST)) you just need to rewrite that if(isset($_POST[$item])), I had help and this is not checking for posted data just so you need to add the '$item' so it checks for the 'username'. Then I had misspelled token in the form as tokin. So that fixed the require_once(classes/Toki(should be e not i)n.php) problem.

<form action="" method="post">
  <div class="field">
    <label for="username">Username</label>
    <input type="text" name="username" id="username" value="<br />
<b>Notice</b>:  Undefined index: username in <b>C:\xampp\htdocs\PHP_Registration_Login\classes\Input.php</b> on line <b>23</b><br />
" autocomplete="off">
  </div>
  <div class="field">
    <label for="password">Choose a password</label>
    <input type="password" name="password" id="password" value="">
  </div>
  <div class="field">
    <label for="password_again">Confirm password</label>
    <input type="password" name="password_again" id="password_again" value="">
  </div>
  <div class="field">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" value="<br />
<b>Notice</b>:  Undefined index: name in <b>C:\xampp\htdocs\PHP_Registration_Login\classes\Input.php</b> on line <b>23</b><br />
">
  </div>

  <input type="hidden" name="token" value="<br />
<b>Warning</b>:  require_once(classes/Tokin.php): failed to open stream: No such file or directory in <b>C:\xampp\htdocs\PHP_Registration_Login\core\init.php</b> on line <b>22</b><br />
<br />
<b>Fatal error</b>:  require_once(): Failed opening required 'classes/Tokin.php' (include_path='.;C:\xampp\php\PEAR') in <b>C:\xampp\htdocs\PHP_Registration_Login\core\init.php</b> on line <b>22</b><br />

Here is init.php

<?php
session_start();

$GLOBALS['config'] = array(
  'mysql' => array(
    'host' =>  '127.0.0.1',
    'username' => 'root',
    'password' => 'root',
    'db' => 'register_login'
  ),
  'remember' => array(
    'cookie_name' => 'hash',
    'cookie_expiry' => 604800
  ),
  'session' => array(
    'session_name' => 'user',
    'token_name' => 'token'
  )
);

spl_autoload_register(function($class) {
  require_once 'classes/' . $class . '.php';
});

require_once 'functions/sanitize.php';
 ?>

Here is register.php

<?php
require_once 'core/init.php';

if(Input::exists()){
  if(Token::check(Input::get('token'))){
    $validate = new Validate();
    $validation = $validate->check($_POST, array(
      'username' => array(
        'required' => true,
        'min' => 2,
        'max' => 20,
        'unique' => 'users'
      ),
      'password' => array(
        'required' => true,
        'min' => 6
      ),
      'password_again' => array(
        'required' => true,
        'matches' => 'password',
      ),
      'name' => array(
        'required' => true,
        'min' => 2,
        'max' => 50
      )
    ));

    if($validation->passed()) {
      Session::flash('success', 'You registered successfully!');
      header('Location: index.php');
    } else {
      foreach($validation->errors() as $error){
        echo $error, '<br>';
      }
    }
  }
}
?>

<form action="" method="post">
  <div class="field">
    <label for="username">Username</label>
    <input type="text" name="username" id="username" value="<?php echo escape(Input::get('username')); ?>" autocomplete="off">
  </div>
  <div class="field">
    <label for="password">Choose a password</label>
    <input type="password" name="password" id="password" value="">
  </div>
  <div class="field">
    <label for="password_again">Confirm password</label>
    <input type="password" name="password_again" id="password_again" value="">
  </div>
  <div class="field">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" value="<?php echo escape(Input::get('name')); ?>">
  </div>

  <input type="hidden" name="token" value="<?php echo Tokin::generate(); ?>">
  <input type="submit" value="Sign Up">
</form>

Here is input.php

<?php

class Input {


  public static function exists($type = 'post') {
    switch($type) {
      case 'post';
        return (!empty($_POST)) ? true : false;
      break;
      case 'get';
        return (!empty($_GET)) ? true : false;
      break;
      default:
        return false;
      break;
    }
  }


  public static function get($item){
    if(isset($_POST)){
      return $_POST[$item];
    } elseif(isset($_GET)) {
        return $_GET[$item];
    }
    return '';
  }

}

Here is Token.php

class Token {
  public static function generate() {
    return Session::put(Config::get('session/token_name'), md5(uniqid()));
  }

  public static function check($token) {
    $tokenName = Config::get('session/toke_name');

    if(Session::exists($tokenName) && $token === Session::get($tokenName)){
      Session::delete($tokenName);
      return true;
    }
    return false;
  }
}

And here is Session.php since Token.php uses it

<?php
class Session {

  public static function exists($name) {
    return(isset($_SESSION[$name])) ? true : false;
  }

  public static function put($name, $value){
    return $_SESSION[$name] = $value;
  }

  public static function get($name) {
    return $_SESSION[$name];
  }

  public static function delete() {
    if(self::exists($name)) {
      unset($_SESSION[$name]);
    }
  }

  public static function flash($name, $string = '') {
    if(self::exists($name)){
      $session = self::get($name);
      self::delete($name);
      return $session;
    } else {
      self::put($name, $string);
    }
  }


}
NewbieCoder
  • 111
  • 4
  • 15
  • Where is the duplicate post? I tried looking for this answer but could not find. If you could redirect me I would be happy to look! – NewbieCoder Dec 12 '15 at 16:01
  • Click on the blue link in the yellow box at the top of your question – RiggsFolly Dec 12 '15 at 16:04
  • @john Conde I do not think that is the same question I am asking. If you look at my code I already use the isset() to check if a $_POST or $_GET was set. If it is then it returns the get or post data and if not it returns an empty string. I don't think that is the same , but I am no pro. I did not get the answer I needed from that post – NewbieCoder Dec 12 '15 at 16:10
  • @RiggsFolly Didn't help I did not get Undefined variable. – NewbieCoder Dec 12 '15 at 16:11
  • I edited to point out more of what I am having problems with. – NewbieCoder Dec 12 '15 at 16:13
  • There's way too much code. You *must* post a minimal example demonstrating the problem. You clearly have done no debugging on your own. – John Conde Dec 12 '15 at 16:20
  • It is as stated the same error. But here is a hint. The first time this page runs its presumably from a click on a link or maybe by you keying in the url in the browser. In these cases the form was not sent back to the server for processing and the $_POST variables are not set. These only get set when a user presses the submit button. So you have to code this into your page i.e. on first execution no data will be available. Look at **Ways to deal with the issue:** in the linked duplicate – RiggsFolly Dec 12 '15 at 16:23
  • @RiggsFolly The submit button is not even showing up. – NewbieCoder Dec 12 '15 at 16:26
  • _You are missing the point_ To fix this error read the linked post. **Not showing the button** thats because you have other errors as well before the code gets to outputting the part of the HTML that outputs the button – RiggsFolly Dec 12 '15 at 16:28
  • Oh and by the way, there are a million tutorials out there on the web, every muppet that want to publicise there skills or web site can write one, _A few of them are very good????_ Hint: If the tutorial says anything about turning error reporting OFF, Run for the hills – RiggsFolly Dec 12 '15 at 16:31
  • @RiggsFolly From the post for Notice: Undefined Index, It states when using a form and you have
    ... it wont work because the value has not been set , but in my input class I have the conditional if to check if it is set. And if it is not then it should return ' ' , or a blank string. I am copying of a tutorial and i have the exact code. So I am still confused and not sure where to go from there.
    – NewbieCoder Dec 12 '15 at 16:37
  • _but in my input class I have the conditional if to check if it is set._ Yes, but you output the HTML regardless of that test. **And this line** `` **Does not check for the existance of `username`** It just checks for the existance of $_POST Not the same thing – RiggsFolly Dec 12 '15 at 16:42
  • Okay I get what you are saying, but in the tutorial for Codecourse: PHP OOP Login/Register System , that is the exact way they do it. Is there some way to do the checking without putting a condition if(isset($_POST))? I guess what I am asking is why do they not get the error if this is the case? – NewbieCoder Dec 12 '15 at 16:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97718/discussion-between-newbiecoder-and-riggsfolly). – NewbieCoder Dec 12 '15 at 16:52
  • I have added the answer in the edit section in the top! – NewbieCoder Dec 12 '15 at 18:34

0 Answers0