ryanmetzler3 Posted August 21, 2013 Share Posted August 21, 2013 I'm building this site which allows users to register and log in. There will be a monthly give away for the best photo upload. The purpose of users is to know the email address corresponding to the winning photo. But I want all features of the site available to everyone. People who are not registered just wont be entered into the giveaway when they upload. That being said, how can I simply display on the homepage their username if they are logged in, or display the log in form otherwise. Here is my log in script below. <?php error_reporting(E_ALL^ E_NOTICE); session_start(); $username = $_SESSION['username']; $userid = $_SESSION['userid']; ?> <html> <head> </head> <body> <?php if ($username && $userid) { echo "You are already logged in as <b>$username</b>"; echo "<br/>Not " . $username . "? <a href='logout.php'>Logout</a>"; } else { $form = "<form action='login.php' method='post'> <table> <tr> <td>Username:</td> <td><input type='text' name='user'/></td> </tr> <tr> <td>Password:</td> <td><input type='password' name='password'/></td> </tr> <tr> <td></td> <td><input type='submit' name='loginbtn' value='Login'/></td> </tr> <tr> <td><a href='register.php'>Register</a></td> <td><a href='forgotpass.php'>Forgot Password?</a></td> </tr> </table> </form>"; } if ($_POST['loginbtn']) { $user = $_POST['user']; $password = $_POST['password']; if ($user) { if ($password) { require ("connect.php"); $password = md5(md5("R4E2M0".$password."R4E2M0")); $query = mysql_query("SELECT * FROM user WHERE username='$user'"); $numrows = mysql_num_rows($query); if ($numrows ==1) { $row = mysql_fetch_assoc($query); $dbid= $row['id']; $dbuser= $row['username']; $dbpass = $row['password']; $dbactive = $row['active']; if ($password == $dbpass) { if ($dbactive == 1) { // set session info $_SESSION['userid'] = $dbid; $_SESSION['username'] = $dbuser; echo "You have been logged in as <b>$dbuser</b>. <a href='member.php'>Click Here</a> to go to the member page.</a>"; } else { echo "You must activate your account to login. $form"; } } else { echo "You did not enter the correct password. $form"; } } else { echo "The username you entered was not found.$form"; } mysql_close(); } else { echo "You must enter your password . $form"; } } else { echo "You must enter your username . $form"; } } else{ echo $form; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
fastsol Posted August 21, 2013 Share Posted August 21, 2013 What exactly is the issue you have? You have the basic concept of what to do using a if, else block. Are you getting errors? Your session_start() should be above the error_reporting() also or you'll get a header warning when ever a error is thrown from something else on the page. Quote Link to comment Share on other sites More sharing options...
ryanmetzler3 Posted August 21, 2013 Author Share Posted August 21, 2013 What exactly is the issue you have? You have the basic concept of what to do using a if, else block. Are you getting errors? Your session_start() should be above the error_reporting() also or you'll get a header warning when ever a error is thrown from something else on the page. I will try to further explain my question. I have no problems with my log in script that I wrote, it works. When I go to localhost/login.php it tells me that I am logged in or allows me to log in. But what kind of if statement can I write into the index.php (homepage) to display either that the user is logged in or that they need to log in. For example on Youtube it displays your username on the top right corner if you are logged in. Or if you are not it displays a link to log in. This probably seems like a dumb question, but I just learned how to create and register users from a tutorial and don't have a complete understanding of sessions. I don't know what the purpose of the following block of code was: <?php error_reporting(E_ALL^ E_NOTICE); session_start(); $username = $_SESSION['username']; $userid = $_SESSION['userid']; ?> Quote Link to comment Share on other sites More sharing options...
Solution fastsol Posted August 21, 2013 Solution Share Posted August 21, 2013 This block of code is what is determining what to show, so put this on whatever page you want the same logic, although putting it in a separate file and including it would be the best. if ($username && $userid) { echo "You are already logged in as <b>$username</b>"; echo "<br/>Not " . $username . "? <a href='logout.php'>Logout</a>"; } else { $form = "<form action='login.php' method='post'> <table> <tr> <td>Username:</td> <td><input type='text' name='user'/></td> </tr> <tr> <td>Password:</td> <td><input type='password' name='password'/></td> </tr> <tr> <td></td> <td><input type='submit' name='loginbtn' value='Login'/></td> </tr> <tr> <td><a href='register.php'>Register</a></td> <td><a href='forgotpass.php'>Forgot Password?</a></td> </tr> </table> </form>"; } Now you also need to have this bit of code present on the page before the other code for it to work. session_start(); error_reporting(E_ALL^ E_NOTICE); $username = $_SESSION['username']; $userid = $_SESSION['userid']; Notice I also changed the session_start() like I posted before. Quote Link to comment Share on other sites More sharing options...
ryanmetzler3 Posted August 21, 2013 Author Share Posted August 21, 2013 Thank so much. Helped a lot 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.