November 29, 201411 yr $_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: Notice: 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!
November 30, 201411 yr Hi! In this part : if($row = $query->fetch()){ $_SESSION['user'] = $row['user']; $_SESSION['pass'] = $row['pass']; header("Location: ".$_SERVER['PHP_SELF']); exit(); } You have a header function, after the page is redirected to the same url, the post will be empty. Isn't is possible that you have a successful login?
December 7, 201411 yr Author Hi, thank you for your attention! echo $_SESSION['user'] doesn't show To test it I made test.php: <?php session_start(); echo $_SESSION['user']; ?> It shows empty page. But without session_start(): Notice: Undefined variable: _SESSION NOCK, I'm very new to this php field, to debug. I even don't know how to do it in general. So this example which I'm learning is simple and sometimes it works.
December 15, 201411 yr It's a bit of a pickle here and a lot of your php is in the wrong place which will give double headers. In addition the html on the code is incorrect and although I write procedural (rather than OOP) I can't see any kind of data cleansing before inserting into the db? You might also want to look at what happens if the input field is blank, there's not conditional checks for the user and password. Try running this in a test.php simply for creating the session. <?php session_start(); if($_SERVER['REQUEST_METHOD']=='POST') { // If it has been posted $uname = trim($_POST['user']); $pword = trim($_POST['pass']); // Check if the fields are empty if($uname=="" || $pword=="") { $errmsg = "Please enter a username and password"; } else { $_SESSION["user"] = $uname; $_SESSION["pass"] = $pword; } } ?> <!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <?php if(isset($errmsg)) { echo "<p>" . $errmsg . "</p>"; } ?> <?php if(isset($_SESSION["user"]) && isset($_SESSION["user"])) { echo "<p>Session Set for user " . $_SESSION["user"] . " and password " . $_SESSION["pass"] . "</p>"; } ?> <form action="test.php" method="post"> <ul> <li>User: <input type="text" name="user"></li> <li>Password: <input type="pass" name="pass"></li> <li><input type="submit"></li> </ul> </form> </body> </html>
March 7, 201511 yr Author I've marked as solved by BrowserBugs. (i thank you for your attention & code) Answer is simple to give different forms different names in html. <input type="text" name="user1"> AND if(isset($_POST['user1']) name user $_POST[use and that's all Edited March 7, 201511 yr by xcislav
Create an account or sign in to comment