Boldonglen Posted March 11, 2011 Share Posted March 11, 2011 Hello firstly i would like to say im very new to this forum (Signed up 5 minutes ago). I have decided to join as im in my final year at university and have been asked to create a website, so im guessing ill be coming back here a lot for help! . Anyway my most current problem is i have an if statement that hides the "Register" button if the user is logged in and Shows a welcome message and the users name if the user is logged in. However i would like to make the log in form disappear if the user is logged in. I know that by putting the form code in the else part of my if statement it will disappear if the user is logged in however when i do this it stops my whole website working. Any help would be very grateful. Thanks Glen. Quote Link to comment Share on other sites More sharing options...
.josh Posted March 11, 2011 Share Posted March 11, 2011 Your problem is on line 42 Quote Link to comment Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 Your problem is on line 42 Lol, yeah we need the codes. Quote Link to comment Share on other sites More sharing options...
searls03 Posted March 11, 2011 Share Posted March 11, 2011 would help if we had code....but If you are going to "echo" a form like you echo if they are logged in....you would but a \before any " in the code as long as the echo is enclosed in ". Dont know if this helps, but hopefully it does! Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted March 11, 2011 Author Share Posted March 11, 2011 Im sorry im new to this forum forgot to put the code in Here is my code: <?php session_start(); if ($_SESSION['username']) echo "Welcome, ".$_SESSION['username']."! <br><a href='logout.php'>Logout!</a>"; else echo "<a href='register.php'>Register?</a>"; ?> <form action='login.php' method="post"> Username: <input type="text" name="username"> <br> Password: <input type="password" name="password"> <br> <input type="submit" value="Log in"> </form> I would like to put the form above into the else part of the if statement. Quote Link to comment Share on other sites More sharing options...
searls03 Posted March 11, 2011 Share Posted March 11, 2011 I don't know if this will work, but try: <?php session_start(); if ($_SESSION['username']) echo "Welcome, ".$_SESSION['username']."! <br><a href='logout.php'>Logout!</a>"; else echo "<a href='register.php'>Register?</a>";\ echo " <form action=\"login.php\" method=\"post\"> Username: <input type=\"text\" name=\"username\"> <br> Password: <input type=\"password\" name=\"password\"> <br> <input type=\"submit\" value=\"Log in\"> </form> ?> again, I don't know if that is what you want, but it could be...... Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted March 11, 2011 Author Share Posted March 11, 2011 Hi thanks for the help but this still does not work . Quote Link to comment Share on other sites More sharing options...
searls03 Posted March 11, 2011 Share Posted March 11, 2011 are you getting an error or is it just not doing what you want it too. if error, tell us Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted March 11, 2011 Author Share Posted March 11, 2011 When i test the website in a browser the screen is just blank. No error message. Quote Link to comment Share on other sites More sharing options...
searls03 Posted March 11, 2011 Share Posted March 11, 2011 You need to connect to a database first. Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted March 11, 2011 Author Share Posted March 11, 2011 I have connected to a database on another page. Everything works fine before i move the form into the php code i just would like the form to disappear once the user is logged in. Quote Link to comment Share on other sites More sharing options...
searls03 Posted March 11, 2011 Share Posted March 11, 2011 usually you need to connect on every page. otherwise it has no idea what the session is or where to get it from. I would make an include file. Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted March 11, 2011 Author Share Posted March 11, 2011 Oh really? Im a little confused as it is working fine without. Quote Link to comment Share on other sites More sharing options...
searls03 Posted March 11, 2011 Share Posted March 11, 2011 try something like: <?php session_start(); // Must start session first thing // Here we run a login check if (!isset($_SESSION['id'])) { echo 'Please <a href="login.php">log in</a> to access your account'; exit(); } //Connect to the database through our include include_once "connect_to_mysql.php"; // Place Session variable 'id' into local variable $id = $_SESSION['id']; ?> Try something like this and see if it works. Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted March 11, 2011 Author Share Posted March 11, 2011 But my code is working fine to connect to a database. I do not need that previous code. I only need to know how to hide the login form if the user is logged in. Quote Link to comment Share on other sites More sharing options...
searls03 Posted March 11, 2011 Share Posted March 11, 2011 just try to stick that code in at the beginning of the page, but you need to create a file called connect_to_mysql.php: <?php // Place db host name. Usually is "localhost" but sometimes a more direct string is needed $db_host = "db_host"; // Place the username for the MySQL database here $db_username = "username"; // Place the password for the MySQL database here $db_pass = "password"; // Place the name for the MySQL database here $db_name = "dbname"; mysql_connect("$db_host","$db_username","$db_pass") or die(mysql_error()); mysql_select_db("$db_name") or die("no database by that name"); ?> I understand that a previous page may work, but the page that you put the connection on is the only one that is gonna connect. You need to put a connect on everypage as I have always been told. Quote Link to comment Share on other sites More sharing options...
.josh Posted March 11, 2011 Share Posted March 11, 2011 If you want to include more than one line of code in a condition you must wrap it in { ... } <?php session_start(); if ($_SESSION['username']) { echo "Welcome, ".$_SESSION['username']."! <br><a href='logout.php'>Logout!</a>"; } else { ?> <a href='register.php'>Register?</a> <form action='login.php' method="post"> Username: <input type="text" name="username"> <br> Password: <input type="password" name="password"> <br> <input type="submit" value="Log in"> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted March 11, 2011 Share Posted March 11, 2011 I understand that a previous page may work, but the page that you put the connection on is the only one that is gonna connect. You need to put a connect on everypage as I have always been told. You only need to have connection code on a page where you are actually wanting to make use of a database. Judging by his code, he has his connection code, authenticates the user with it, and if everything is valid, he starts a session variable. Then on subsequent pages he checks if that session variable exists. You do not need to have database connection code on every page for this. Sessions and databases are two different things. Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted March 13, 2011 Author Share Posted March 13, 2011 Thank you Crayon Violent This code works great! Big help. Thanks again. I will come back if i need anymore help. Thanks!!!! 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.