Support me in doing my very first steps, Please. Previously I've had two links to register.php and login.php. They had header("Location: index.php");
unfamiliar to me. And I've found header("Location: ".$_SERVER['PHP_SELF']);
index.php:
<!DOCTYPE html>
<html>
<h1>Register</h1>
<form method="POST">
<input type="text" name="user"><br /><br />
<input type="pass" name="pass"><br /><br />
<input type="submit"><br />
</form>
<?php
session_start();
if(isset($_POST['user'], $_POST['pass'])){
require 'connect.php';
$zr++;
$query = d()->prepare("INSERT INTO u (user, pass, loc) VALUES (:user, :pass, :loc)");
$query->bindParam(':user', $_POST['user']);
$query->bindParam(':pass', $_POST['pass']);
$query->bindParam(':loc', $zr);
if($query->execute()){
$_SESSION['user'] = $row['user'];
$_SESSION['pass'] = $row['pass'];
header("Location: ".$_SERVER['PHP_SELF']);
} else{
echo 'ERROR';
}
}
?>
<h1>Login</h1>
<form method="POST">
<input type="text" name="user"><br /><br />
<input type="pass" name="pass"><br /><br />
<input type="submit"><br />
</form>
<?php
echo $_POST['user'];
if(isset($_POST['user'], $_POST['pass'])){
require 'connect.php';
$query = d()->prepare("SELECT user, pass FROM u WHERE user=:user AND pass=:pass");
$query->bindParam(':user', $_POST['user']);
$query->bindParam(':pass', $_POST['pass']);
$query->execute();
if($row = $query->fetch()){
$_SESSION['user'] = $row['user'];
$_SESSION['pass'] = $row['pass'];
header("Location: ".$_SERVER['PHP_SELF']);
}
}
$us=$_SESSION['user'];
echo 'user ',$us;
?>
<?php
if(isset($_SESSION['user'])){
$us=$_SESSION['user'];
echo '<br /> user ',$us, ' ', '<a href="logout.php">Logout</a>';
echo '<br />', '<a href="zrs.php">zero session</a>';
}
?>
</html>
connect.php:
<?php
function d(){
try{
$db = new pdo("mysql:host=localhost;dbname=tx;","root","hyuiuik");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
} catch(PDOException $e){
echo 'ERROR', $e->getMessage();
}
}
?>
Sometimes $_SESSION doesn't work when I write code. And $_POST didn't work at all. Undefined index: user in /var/www/localhost/htdocs/index.php on line 56
It's a learning example in which I'm asking for help, for those who emplemented the login system. Let's consider this code an a critical minimalistic approach. That's exactly why I need to have one and only one page log/reg system.
In multifile version it works. I could only see header(Location: ("the why").
Does anyone know or guess the way to make one-page in order to ask the people who know this system to say their tech view somehow?