3 Replies - 192 Views - Last Post: 11 December 2014 - 09:45 AM Rate Topic: -----

#1 kscislav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-December 14

$_POST code with login and registration

Posted 10 December 2014 - 11:20 PM

$_POST can't be seen in the second part of login (after the registration part) in one-page authorization.

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" name="ok1"><br />
</form>
<?php
session_start();
if(isset($_POST['user'], $_POST['pass'])){
require 'connection.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']);
exit();
} 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" name="ok2"><br />
</form>

<?php
if(isset($_POST['user'], $_POST['pass'], $_POST['ok2'])){
require 'connection.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']);
exit();
}
echo $_POST['user'];
$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>';

} else
{
echo '<br /> <a href="register.php">Register</a>';
}
?>

</html>

connection.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();
        }
    }
?>


I was advised to put echo $_POST['user'] inside isset () check because of an error:

Undefined index: user in /var/www/localhost/htdocs/index.php on line 56


That advice wasn't helpful at all. And I've returned back the echo $_POST['user'] - to see error and think of it again.
I also assigned a submit button a name "ok1" and "ok2". The second submit is checked again for pressing after the second form.

So the $_POST doesn't show up while the $_SESSION is ok (as the data adds to MySQL).

Help me with the login+registration page please! (because I feel abashed when many files around in OS). I want to operate $_POST and $_SESSION for my the simplest game in JS.

_
thanks in advance to all of you
and bless you goodness!

Is This A Good Question/Topic? 0
  • +

Replies To: $_POST code with login and registration

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: $_POST code with login and registration

Posted 11 December 2014 - 01:48 AM

lines #10 & #47 don’t work (and also emit a notice).

the try…catch from line #76 is misplaced, if you had a connection failure, your programme (thanks to the caught exception) will proceed as if the connection were successful hence giving you a 'method call on a non-object error'.

the code from line #36 on will not be executed since the second form is already dealt with by the code from line #11.

line #51 will throw the observed notice since this code is also executed on GET requests (the initial load).
Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 1,816
  • Joined: 15-January 14

Re: $_POST code with login and registration

Posted 11 December 2014 - 09:30 AM

Quote

That advice wasn't helpful at all.

The advice is correct. This is how to do it:

if (isset($_POST['user'])) {
  echo $_POST['user'];
}

Now you won't get the notice if $_POST doesn't have a user element.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: $_POST code with login and registration

Posted 11 December 2014 - 09:45 AM

another option were:
echo filter_input(INPUT_POST, 'user', FILTER_SANITIZE_STRING);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1