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?

session_start() needs to be the first line in every page using sessions.

example:

newpage.php

<?php 
    session_start();
       code
       code
       code
       code
       code
       code
    session_write_close();

closing the session write when you are done writing to the session file, helps with proformance.

index undefined: user means, you are asking php for $_POST['user'] when it doesnt exist. To help with this, check if the index is set.

example

if( isset($_POST['user'] ){
    code
    code
    code
    code
}else{
    other code
    other code
    other code
}

the header function will redirect user to the location specified in the string

example:

if( !isset($_POST['user']) ){
    header("location: http://" . $_SERVER['HTTP_HOST'] . "/login.php");
}else{
    //user is set

    run code
    run code

}

Try this index and connection.php i dont have database created so it wont work .

<!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
$host = "localhost";
$user = "user";
$pass = "pass";

  if(isset($_POST['user'], $_POST['pass'])){
  $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
require 'connect.php';
$user = $_POST['user'];
echo $_POST['user'];
  if(isset($_POST['user'], $_POST['pass'])){

  $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>

Connection.php

<?php

$host= "localhost";
$user ="user";
$pass = "pass";
try {
    $db = new PDO("mysql:host=$host;dbname=tx", $user, $pass);
    // set the PDO error mode to exception
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.