Guestbook with Login

I have a problem regarding to the condition.
if the user is not logged in,
	shoe the log in form
	only 3 guestbook comments will show
	the guestbook form will not be available
if the user is not logged in,
	show the logout button
	show all guestbook comments
	the log in form will not be available
help me or try to give other answers
here is the code
  1. <html>
  2. <head>
  3. <style>
  4. .error {color: #FF0000;}
  5. </style>
  6. </head>
  7. <body>
  8.  
  9. <?php
  10. $con = mysqli_connect("127.0.0.1","root","","");
  11. echo " failed" . mysqli_conect_error();
  12. }else{
  13. echo "connect";
  14. }
  15.  
  16.  
  17. $nameErr = $ageErr = $emailErr = "";
  18. $name = $age = $email = $comment = "";
  19.  
  20. if ($_SERVER["REQUEST_METHOD"] == "POST")
  21. {
  22. if (empty($_POST["name"]))
  23. {
  24. $nameErr = "Name is required";
  25. }
  26. else
  27. {
  28. $name = test_input($_POST["name"]);
  29. if (!preg_match("/^[a-zA-Z ]*$/",$name))
  30. {
  31. $nameErr = "Only letters and white space allowed";
  32. }
  33. }
  34.  
  35. if (empty($_POST["email"]))
  36. {
  37. $emailErr = "Email is required";
  38. }
  39. else
  40. {
  41. $email = test_input($_POST["email"]);
  42. if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
  43. {
  44. $emailErr = "Invalid email format";
  45. }
  46. }
  47.  
  48.  
  49. if (empty($_POST["comment"]))
  50. {
  51. $comment = "";
  52. }
  53. else
  54. {
  55. $comment = test_input($_POST["comment"]);
  56. }
  57. if (empty($_POST["age"])){
  58. $age = "";
  59. }
  60. else
  61. {
  62. $age = test_input($_POST["age"]);
  63. }
  64. }
  65.  
  66.  
  67. function test_input($data)
  68. {
  69. $data = trim($data);
  70. $data = stripslashes($data);
  71. $data = htmlspecialchars($data);
  72. return $data;
  73. }
  74. ?>
  75. <p><span class="error">* required field.</span></p>
  76. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  77. Name: <input type="text" name="name" value="<?php echo $name;?>">
  78. <span class="error">* <?php echo $nameErr;?></span>
  79. <br><br>
  80. Age: <input type="text" name="age" value="<?php echo $age;?>">
  81. <span class="error">*<?php echo $ageErr;?></span>
  82. <br><br>
  83. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  84. <span class="error">* <?php echo $emailErr;?></span>
  85. <br><br>
  86. Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  87. <br><br>
  88. <input type="submit" name="submit" value="Send Comment">
  89. </form>
  90.  
  91. <?php
  92.  
  93.  
  94. echo "<h2>Your Input:</h2>";
  95. echo $name;
  96. echo "<br>";
  97. echo $age;
  98. echo "<br>";
  99. echo $email;
  100. echo "<br>";
  101. echo $comment;
  102. echo "<br>";
  103. ?>
  104.  
  105. </body>
  106. </html>

Add new comment