Hi everyone, I've gotten a login setup on a site and it displays somewhere on every page. What i am trying to do is when someone logins I would like it to be switched to a little control panel showing your name and other things that i am able to setup. I'm just not sure how to switch them, maybe this will help you get an idea of what i mean: if (logged in) { show control panel } else show login Must i use javascript for this?
I think using javascript isn't good option. If the client had turned off javascript then it becomes problem. He cant see any control panel.
If you login, either a cookie, or session variable is set. E.g. session_start(); if(login_valid()) { $_SESSION['name']=$_POST['loginname']; } Code (markup): You only have got to check if this variable is set if(isset($_SESSION['name'])) { echo "Your name is: ".$_SESSION['name']; } Code (markup):
Thank you for the reply. I have a simple login i'm testing with. Which simply is: <form action='login.php' method='post'> <input type="text" name="username" /><br> <input type="password" name="password" /><br> <input type="submit" value="Log in" /> </form> I want this only to show if the user is logged in. If not I want something else shown. Not sure how to do this though. So it'll end up as something like (i think): if(isset($_SESSION['username'])) { echo "welcome"; } else *display the login form* Not sure how to do this though :s
The login.php is very simple as well right now: <?php session_start (); $username = $_POST['username']; $password = $_POST['password']; if ($username && $password){ $connect = mysql_connect("localhost","root","") or die ("Couldnt connect!"); mysql_select_db("phplogin") or die ("Couldn't find the DB"); $query = mysql_query ("SELECT username && password FROM users WHERE username='$username'"); echo "hello"; } else echo "no username";
Bump. I'm still looking for an answer please. Forget the code it's definitely not something i would use on a site it was just a very simple write up to test this on. All i want to do is remove HTML elements if a user is logged in. The only way I know how is to create 2 pages, so 2 index pages. Once the user is logged in the user will have access to the second index page which does not contain the form. There has to be a better way of doing this though. How can I make it so that the form is removed once the user logs in?
you want to remove login form or login form link from page? if i am correct then use this. <? if($session->logged_in){?> <a href="">Control panel</a> <? } else { ?> <a href="">Login</a> | <a href="">Register</a> hope this will help. Thanks
If you want to write your own code in Javascript, you'll need a Javascript variable to tell you whether the user has logged in. So in your PHP login, once you ascertain that you get one (and only 1) record back from your select statement echo '<script>'; // if the user is logged in echo 'var loggedIn = true;'; //else echo 'var loggedIn = false;'; echo '</script>'; Code (markup): Then your Javascript code just has to see if loggedIn is true or false.