Please tell me how to implement the form hiding after authorization without using JS. At the moment after logging in, it is located below the "Write Post" form. (the form "Write post" appears after successful login) Code:

<html> <head> <title> Log In</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link href="style.css" rel="stylesheet"> </head> <body bgcolor="#999"> <?php include("config.php"); if(isset($_POST['submit']) && $_POST['submit']=="submit"){ $sql = "INSERT INTO post (title, content) values ('".$_POST['title']."', '".$_POST['post']."')"; $stmt = $conn->prepare($sql); $stmt->execute(); sleep(3); header("location:index.php"); } if(isset($_POST['login']) && $_POST['login']=="LOGIN"){ $sql = "SELECT * FROM admin where user='".$_POST['user']."' and password='".md5($_POST['password'])."'"; $stmt = $conn->prepare($sql); $stmt->execute(); sleep(2); $log = $stmt->rowCount(); } else { $log=0; } if($log==1){ echo "<center>Welcome, ".$_POST['user']." </center>"; ?> <div class="postform"> <form method="POST" action="" class="formpost"> Title:<input class="title" type="text" name="title"> Content: <textarea rows="5" cols="25" class="textarea" name="post"></textarea><br> <tr> <td colspan="2" style="text-align:right;"><input class="submit" type="submit" name="submit" value="submit"></td> </tr> </form> </div> <?php } else { echo ""; } ?> <div class="loginform"> <form method="POST" action=""> <table> <tr> <td>Username:</td> <td><input name="user"></td> </tr> <tr> <td>Password:</td> <td><input name="password" type="password"></td> </tr> <tr> <td colspan="2" style="text-align:right;"><input type="submit" name="login" value="LOGIN"></td> </tr> </table> </form> </div> </body> </html> 

enter image description here

    1 answer 1

    If I understand you correctly and understand the code correctly, then you have authorization status in $ log.

     $log == 1 // авторизован $log == 0 // не авторизован 

     <html> <head> <title> Log In</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link href="style.css" rel="stylesheet"> </head> <body bgcolor="#999"> <?php include("config.php"); if(isset($_POST['submit']) && $_POST['submit']=="submit"){ $sql = "INSERT INTO post (title, content) values ('".$_POST['title']."', '".$_POST['post']."')"; $stmt = $conn->prepare($sql); $stmt->execute(); sleep(3); header("location:index.php"); } if(isset($_POST['login']) && $_POST['login']=="LOGIN"){ $sql = "SELECT * FROM admin where user='".$_POST['user']."' and password='".md5($_POST['password'])."'"; $stmt = $conn->prepare($sql); $stmt->execute(); sleep(2); $log = $stmt->rowCount(); } else { $log=0; } if($log==1){ echo "<center>Welcome, ".$_POST['user']." </center>"; ?> <div class="postform"> <form method="POST" action="" class="formpost"> Title:<input class="title" type="text" name="title"> Content: <textarea rows="5" cols="25" class="textarea" name="post"></textarea><br> <tr> <td colspan="2" style="text-align:right;"><input class="submit" type="submit" name="submit" value="submit"></td> </tr> </form> </div> <?php } else {?> <div class="loginform"> <form method="POST" action=""> <table> <tr> <td>Username:</td> <td><input name="user"></td> </tr> <tr> <td>Password:</td> <td><input name="password" type="password"></td> </tr> <tr> <td colspan="2" style="text-align:right;"><input type="submit" name="login" value="LOGIN"></td> </tr> </table> </form> </div> <?php } ?> </body> </html> 

    • Yes that's right. $ log == 1 // authorized. Thanks, helped! - Isabella Monza
    • one
      It should be noted that when using php in html, it is better to use constructions of the form <?php if($log==1) : ?> , <?php else : ?> , <?php endif; ?> <?php endif; ?> , <?= $_POST['user']; ?> <?= $_POST['user']; ?> , etc. This will significantly increase the readability of the code. - Pyramidhead