php - Have to reload page for if statement to be parsed -
first: code!
loginform.html
<form action="" method="post" id="loginform"> <h3>login</h3> <input type="text" name="username" placeholder="username"> <br> <br> <input type="password" name="password" placeholder="password"> <br> <input type="submit" name="logsubmit" value="login" class="registerbutton"> </form>
login.php
<?php require_once("../resources/config.php"); require_once("../resources/library/dbconnect.php"); function checkuser($con) { if (isset($_post['username']) && isset($_post['password'])){ $username = $_post['username']; $pw = md5($_post['password']); $sql="select * `users` username='$username' , pw='$pw'"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($result); if ($result==true && $username == $row["username"] && $pw==$row["pw"]) { $_session["logged_in"] = 1; $_session["admin"] = $row["admin"]; $_session["username"] = $row["username"]; } else { $msg = "das war nichts! passwort oder username falsch? <br>".mysqli_error($con); unset($_session["logged_in"]); } } } checkuser($connection); header('location: ../public_html/index.php'); exit; ?>
index.php
<?php session_start(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>barstone</title> <link rel="stylesheet" href="css/default.php" type="text/css"> <link href='http://fonts.googleapis.com/css?family=bitter:700&subset=latin,latin-ext' rel='stylesheet' type='text/css'> </head> <body> <?php require_once("../resources/config.php"); require_once("../resources/library/header_nav.php");?> <div class="pagewrapper"> <div class="register-container"> <?php if(!isset($_session['logged_in']) || !isset($_post['regsubmit'])) {require_once("../resources/library/registerform.php");} if (isset($_post['regsubmit']) && $_session['register_check']==true) {printf("success! welcome %s!",$_post['username']);} ?> </div> <div class="login-container"> <?php if (isset($_session['logged_in'])) { printf ("<form action='../resources/library/logout.php' method='post' id='loginform'><h3> hello %s! </h3> nice see you!<input type='submit' class='logoutbutton' value='logout'> </form>",$_session["username"]); }?> <?php if (isset($_post["logsubmit"])) {require_once("../resources/library/login.php"); } else { if (!isset($_session['username'])) {require_once("../resources/library/loginform.html");}} ?> </div> <div class="content"> <?php require_once("../resources/library/articles.php");?> </div> </div> </body> </html>
relevant index.php part
{printf ("<form action='../resources/library/logout.php' method='post' id='loginform'> <h3> hello %s!</h3> nice see you! <input type='submit' class='logoutbutton' value='logout'> </form>",$_session["username"]);} ?> <?php if (isset($_post["logsubmit"])) {require_once("../resources/library/login.php");} else {if (!isset($_session['username'])) {require_once("../resources/library/loginform.html");} }?> </div>
what supposed do:
if user not logged in, show loginform.html
. if login button got pressed, use login.php
log user in.
after has used login-form log in website, displays little welcome message , new button logout purposes.
what does:
displaying login form works fine.
after logged in, displays nothing. after reloading page, button there , works fine.
the $_session['logged_in']
variable set login, why page need reload interpret statement correctly?
i admit way isn't 'best practice' , open advice. still learning. :)
for testing: http://hsturnierv2.pixelpioniere.net/public_html/index.php login "test" pw "test"
the problem fact have
header('location: ../public_html/index.php');
instead of:
header('location: ../public_html/index.php');
in login.php. should have capital l. doesn't , no redirection, blank page ...
update
so redirect ok script dies reason. not logical error rather fatal script error kills script before can output more ... code is:
<?php if (isset($_session['logged_in'])) { printf ("<form action='../resources/library/logout.php' method='post' id='loginform'> <h3> hello %s!</h3> nice see you! <input type='submit' class='logoutbutton' value='logout'> </form>",$_session["username"]); } ?> <?php if (isset($_post["logsubmit"])) { require_once("../resources/library/login.php"); }else { if (!isset($_session['username'])) {require_once("../resources/library/loginform.html"); } } ?>
so got it. when logging in index.php requires login.php when done sends relocation header , exits. output has started! have half of page out! header issues error (which not displayed due php.ini settings) , exits. therefore half page.
you should change index.php code to:
<?php session_start(); if (isset($_post["logsubmit"])) { require_once("../resources/library/login.php"); } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>barstone</title> <link rel="stylesheet" href="css/default.php" type="text/css"> <link href='http://fonts.googleapis.com/css?family=bitter:700&subset=latin,latin-ext' rel='stylesheet' type='text/css'> </head> <body> <?php require_once("../resources/config.php"); require_once("../resources/library/header_nav.php");?> <div class="pagewrapper"> <div class="register-container"> <?php if(!isset($_session['logged_in']) || !isset($_post['regsubmit'])) { if (isset($msg)) echo $msg .'<br>'; require_once("../resources/library/registerform.php");} if (isset($_post['regsubmit']) && $_session['register_check']==true) {printf("success! welcome %s!",$_post['username']);} ?> </div> <div class="login-container"> <?php if (isset($_session['logged_in'])) { printf ("<form action='../resources/library/logout.php' method='post' id='loginform'><h3> hello %s! </h3> nice see you!<input type='submit' class='logoutbutton' value='logout'> </form>",$_session["username"]); }else { if (!isset($_session['username'])) {require_once("../resources/library/loginform.html");}} ?> </div> <div class="content"> <?php require_once("../resources/library/articles.php");?> </div> </div> </body> </html>
this prevents output before header , shows error if enter wrong credentials. should change login.php file doesn't redirect every time, rather if login successful, so:
<?php require_once("../resources/config.php"); require_once("../resources/library/dbconnect.php"); function checkuser($con) { if (isset($_post['username']) && isset($_post['password'])){ $username = $_post['username']; $pw = md5($_post['password']); $sql="select * `users` username='$username' , pw='$pw'"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($result); if ($result==true && $username == $row["username"] && $pw==$row["pw"]) { $_session["logged_in"] = 1; $_session["admin"] = $row["admin"]; $_session["username"] = $row["username"]; $msg='ok'; } else { $msg = "das war nichts! passwort oder username falsch? <br>".mysqli_error($con); unset($_session["logged_in"]); } } return $msg } $result = checkuser($connection); if ($msg=='ok') { header('location: ../public_html/index.php'); exit; } ?>
Comments
Post a Comment