bravo81 Posted April 30, 2008 Share Posted April 30, 2008 Hi all, Im trying to create an Login System. the Login.php works as FAR as I know, because it moves onto index.php But it seems to login then logout on index.php Heres my source code.. login.php: <? session_start(); include './includes/db_connect.php'; if (isset($_SESSION['username'])){ header("Location: index.php"); exit(); } if ($_POST['Submit'] && strip_tags($_POST['username']) && strip_tags($_POST['password'])){ $username = addslashes(strip_tags($_POST['username'])); $password = addslashes(strip_tags($_POST['password'])); $sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1"); $login_check = mysql_num_rows($sql); $inf = mysql_fetch_object($sql); if ($login_check == "0"){ $message="Error - Information Invalid"; }elseif ($login_check != "0"){ header("Location: index.php"); } else { $message= "You could not be logged in.<br />"; }} ?> <table width="221" border="0" align="center" cellpadding="0" cellspacing="0" class="login"> <tr> <td> <p align="center" class="login"> <? echo $message; ?> <form action="" method="post"> <div align="center">Username:<br /> <input name="username" type="text" id="username" value="<?php if (strip_tags($_GET['l'])){ echo "$l"; } ?>" size="15" maxlength="40"> <br /> <br /> Password:<br /> <input name="password" type="password" id="password2" value="<?php if (strip_tags($_GET['pw'])){ echo "$pw"; } ?>" size="15" maxlength="40"> <br /> <br /> <input type="submit" name="Submit" value="Login"> </div> </form> </p> <div align="center"></div></td> </tr> </table> Index.php: <? session_start(); include './includes/db_connect.php'; include './includes/counter.php'; include './includes/functions.php'; $username=$_SESSION['username']; $query2=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1"); $fetch2=mysql_fetch_object($query2); $query=mysql_query("SELECT * FROM display WHERE active='1'"); ?> <? // Start Main Content // ?> <br><br><br> <div align="center"> <? while ($row=mysql_fetch_array($query)){ echo " ".$row['title']."\r\n<br> <p>".$row['content']."</p>"; } if ($fetch2->userlevel == "1"){ echo"<b><a href=edit.php><img src=./images/edit.gif border=0></a>"; } ?> The "userlevel=1" bit dont come up after login, thats why I think its logging out. Im not a PHP pro so im not sure, so anyhelp would be appreciated!! URL is: http://southernheavenflorida.com/newshf Thanks in advance, Dean, Quote Link to comment Share on other sites More sharing options...
bravo81 Posted April 30, 2008 Author Share Posted April 30, 2008 Bump. Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 30, 2008 Share Posted April 30, 2008 $_SESSION['username'] isn't defined in login.php Learning simple debugging practices will help solve these on your own. Next time, try echo'ing the variables you expect a certain value from to make sure they actually contain it. Quote Link to comment Share on other sites More sharing options...
bravo81 Posted April 30, 2008 Author Share Posted April 30, 2008 Im not to sure what I need to do still, add: $username=$_SESSION['username']; to my login.php? Quote Link to comment Share on other sites More sharing options...
bravo81 Posted April 30, 2008 Author Share Posted April 30, 2008 Bump2, Sorry..This is important & I need to get it done. Quote Link to comment Share on other sites More sharing options...
revraz Posted April 30, 2008 Share Posted April 30, 2008 You need to set a session variable in your login script then read that session variable on your second script. Quote Link to comment Share on other sites More sharing options...
bravo81 Posted April 30, 2008 Author Share Posted April 30, 2008 Oh dammit, I didnt really understand that either! Sorry haha. Im self-teaching my self PHP at the moment :| But il have a go, I think i get what you mean. Quote Link to comment Share on other sites More sharing options...
bravo81 Posted April 30, 2008 Author Share Posted April 30, 2008 No dice, what do I need to add/remove or edit? Thanks, Dean. Quote Link to comment Share on other sites More sharing options...
bravo81 Posted April 30, 2008 Author Share Posted April 30, 2008 Help Please! Quote Link to comment Share on other sites More sharing options...
bravo81 Posted April 30, 2008 Author Share Posted April 30, 2008 Fixed it myself, thanks anyway. Quote Link to comment Share on other sites More sharing options...
revraz Posted April 30, 2008 Share Posted April 30, 2008 Knew you could figure it out. Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted April 30, 2008 Share Posted April 30, 2008 I had troubles with this when I was starting out, I'm still not a pro but yeah what they were saying is you were trying to use your $_SESSION['username'] before it had any contents so the script wouldnt have worked. Before you write a script I've always found the best way to get a script flowing nicely is to do one section of it and echo ALL of the variables I've defined to make sure there correct then work with that. I'm self teaching as well I'm now working with OOP thats fun I tell you *sigh* haha Good luck with your future PHP'ing! 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.