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);
}
}
}