newbe123 Posted November 26, 2010 Share Posted November 26, 2010 I am trying to create a blog and visitor log, where I should be able to log in as admin and add the item and see the visitor log. I want a password protected administration section and I want to use sessions on the administration section. When I for example click on add post, I want it to log in and go to page addpost.php where I can enter my text. This is what I've done so far. I would like to know what I have done wrong and how I should do to accomplish what I have mentioned above. <?php session_start(); if(isset($_POST['btnLogin']) || isset($_POST['addpost'])){ $txtUserId = $_REQUEST['txtUserId']; $txtPassword = $_REQUEST['txtPassword']; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { if ($_POST['txtUserId'] === 'admin' && $_POST['txtPassword'] === 'abc123') { //$_SESSION['basic_is_logged_in'] = true; echo "you are logged in"; } elseif (empty($txtUserId) || empty($txtPassword)) { echo "fill in username and password"; } elseif ($_POST['txtUserId'] != 'admin'){ echo "wrong username"; } elseif ($_POST['txtPassword'] != 'abc123'){ echo "wrong password"; } } } ?> <?php //if (!isset($_SESSION['basic_is_logged_in']) // || $_SESSION['basic_is_logged_in'] !== true) ?> <html> <head> <title>Main User Page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form method="post" name="frmLogin" id="frmLogin" action="main.php"> <table width="400" border="1" align="center" cellpadding="2" cellspacing="2"> <tr> <td width="150">User Id</td> <td><input name="txtUserId" type="text" id="txtUserId"></td> </tr> <tr> <td width="150">Password</td> <td><input name="txtPassword" type="password" id="txtPassword"></td> </tr> <tr> <td><input type="submit" name="btnLogin" value="Login"></td> </tr> <tr> <td><input type="submit" name="addpost" value="add post" /></td> </tr> <tr> <td><input type="submit" name="showstat" value="Show visitors log" /></td> </tr> <tr> <td><input type="submit" name="blogg" value="To blog (logout)" /></td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
papaface Posted November 27, 2010 Share Posted November 27, 2010 <?php session_start(); if ($_SESSION['basic_is_logged_in'] == true) { echo "you are logged in"; } else { if(isset($_POST['btnLogin']) || isset($_POST['addpost'])){ $txtUserId = $_REQUEST['txtUserId']; $txtPassword = $_REQUEST['txtPassword']; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { if ($_POST['txtUserId'] === 'admin' && $_POST['txtPassword'] === 'abc123') { $_SESSION['basic_is_logged_in'] = true; echo "you are logged in"; } elseif (empty($txtUserId) || empty($txtPassword)) { echo "fill in username and password"; } elseif ($_POST['txtUserId'] != 'admin'){ echo "wrong username"; } elseif ($_POST['txtPassword'] != 'abc123'){ echo "wrong password"; } } } ?> <?php //if (!isset($_SESSION['basic_is_logged_in']) // || $_SESSION['basic_is_logged_in'] !== true) ?> <html> <head> <title>Main User Page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form method="post" name="frmLogin" id="frmLogin" action="main.php"> <table width="400" border="1" align="center" cellpadding="2" cellspacing="2"> <tr> <td width="150">User Id</td> <td><input name="txtUserId" type="text" id="txtUserId"></td> </tr> <tr> <td width="150">Password</td> <td><input name="txtPassword" type="password" id="txtPassword"></td> </tr> <tr> <td><input type="submit" name="btnLogin" value="Login"></td> </tr> <tr> <td><input type="submit" name="addpost" value="add post" /></td> </tr> <tr> <td><input type="submit" name="showstat" value="Show visitors log" /></td> </tr> <tr> <td><input type="submit" name="blogg" value="To blog (logout)" /></td> </tr> </table> </form> </body> </html> <?php } ?> Quote Link to comment Share on other sites More sharing options...
newbe123 Posted November 27, 2010 Author Share Posted November 27, 2010 it stays logged in and do not want to log out? <?php session_start(); if ($_SESSION['basic_is_logged_in'] == true) { echo "you are logged in"; } else { if(isset($_POST['btnLogin']) || isset($_POST['addpost'])){ $txtUserId = $_REQUEST['txtUserId']; $txtPassword = $_REQUEST['txtPassword']; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { if ($_POST['txtUserId'] === 'admin' && $_POST['txtPassword'] === 'abc123') { $_SESSION['basic_is_logged_in'] = true; echo "you are logged in"; } elseif (empty($txtUserId) || empty($txtPassword)) { echo "fill in username and password"; } elseif ($_POST['txtUserId'] != 'admin'){ echo "wrong username"; } elseif ($_POST['txtPassword'] != 'abc123'){ echo "wrong password"; } } } } ?> <html> <head> <title>Main User Page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form method="post" name="frmLogin" id="frmLogin" action="main.php"> <table width="400" border="1" align="center" cellpadding="2" cellspacing="2"> <tr> <td width="150">User Id</td> <td><input name="txtUserId" type="text" id="txtUserId"></td> </tr> <tr> <td width="150">Password</td> <td><input name="txtPassword" type="password" id="txtPassword"></td> </tr> <tr> <td><input type="submit" name="btnLogin" value="Login"></td> </tr> <tr> <td><input type="submit" name="addpost" value="Lägg till post" /></td> </tr> <tr> <td><input type="submit" name="showstat" value="Visa besökslogg" /></td> </tr> <tr> <td><input type="submit" name="blogg" value="Till bloggen (logga ut)" /></td> </tr> </table> </form> <p><a href="logout.php">Logout</a> </p> </body> </html> logout.php <?php session_start(); if (isset($_SESSION['btnLogin'])) { unset($_SESSION['btnLogin']); } header('Location: main.php'); ?> Quote Link to comment Share on other sites More sharing options...
newbe123 Posted November 27, 2010 Author Share Posted November 27, 2010 fixed it and thank you Quote Link to comment Share on other sites More sharing options...
newbe123 Posted November 27, 2010 Author Share Posted November 27, 2010 I have a problem with one page to another page! What I am trying to do is when I am logged in I want to add post and visit visitors log by clicking on the buttons and enter those pages. (you will not be able to visit the pages when you are logged out, just admin) So I don't know how to do this. like: if(isset($_POST['showstat'])) it will log in as it does now and then enters bloggstat.php what should I do? main.php <?php session_start(); if ($_SESSION['basic_is_logged_in'] == true) { echo "you are logged in"; } else { if(isset($_POST['btnLogin']) || isset($_POST['addpost'])){ $txtUserId = $_REQUEST['txtUserId']; $txtPassword = $_REQUEST['txtPassword']; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { if ($_POST['txtUserId'] === 'admin' && $_POST['txtPassword'] === 'abc123') { $_SESSION['basic_is_logged_in'] = true; echo "you are logged in"; } elseif (empty($txtUserId) || empty($txtPassword)) { echo "fill in username and password"; } elseif ($_POST['txtUserId'] != 'admin'){ echo "wrong username"; } elseif ($_POST['txtPassword'] != 'abc123'){ echo "wrong password"; } } } ?> <html> <head> <title>Main User Page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form method="post" name="frmLogin" id="frmLogin" action="main.php"> <table width="400" border="1" align="center" cellpadding="2" cellspacing="2"> <tr> <td width="150">User Id</td> <td><input name="txtUserId" type="text" id="txtUserId"></td> </tr> <tr> <td width="150">Password</td> <td><input name="txtPassword" type="password" id="txtPassword"></td> </tr> <tr> <td><input type="submit" name="btnLogin" value="Login"></td> </tr> <tr> <td><input type="submit" name="addpost" value="add post" /></td> </tr> <tr> <td><input type="submit" name="showstat" value="Show visitors log" /></td> </tr> <tr> <td><input type="submit" name="blogg" value="To blog (logout)" /></td> </tr> </table> </form> </body> </html> <?php } ?> bloggstat.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Blogg - visitor log/title> <link rel="stylesheet" type="text/css" href="stilmall.css" /> </head> <body> <h1>Bloggstat</h1> <form action="main.php" method="post"> <p> <input type="submit" name="back" value="back" /> </p> </form> visitor:<br /><br />--------------------------------------------------<br /> </body> </html> logout.php <?php session_start(); // Begin session // Unset session if user is logged in. if (isset($_SESSION['basic_is_logged_in'])) { unset($_SESSION['basic_is_logged_in']); } // Access login.php, user is logged out header('Location: main.php'); ?> Quote Link to comment Share on other sites More sharing options...
newbe123 Posted November 27, 2010 Author Share Posted November 27, 2010 I know that it should be something like this if(isset($_POST['addpost'])){ header('Location: bloggadd.php'); } and if(isset($_POST['showstat'])){ header('Location: bloggstat.php'); } but I don't know how to enter the code in the main.php because there are several pages that I want to enter from main.php I really appreciate if someone helps me Quote Link to comment Share on other sites More sharing options...
newbe123 Posted November 28, 2010 Author Share Posted November 28, 2010 can someone plz help me with this..... ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.