dennismonsewicz Posted June 1, 2008 Share Posted June 1, 2008 I am not sure why my script is acting weird. It passes the username to the URL but the else statement does not act right. I gotta start going to bed earlier lol. <?php require "includes/sql.php"; $action = $_GET['action']; session_start(); ob_start(); $_SESSION['username'] = stripslashes($_POST['username']); $_SESSION['password'] = stripslashes($_POST['password']); $username = $_SESSION['username']; $password = $_SESSION['password']; switch($action) { case "login": $result = mysql_query("SELECT * FROM users where username = '$username' AND password = '$password'"); $num_rows = mysql_num_rows($result); if($num_rows > 0) { header("location: index.php?username=" . $username . ""); } else { header("location: index.php"); } break; case "logout": session_destroy(); header("location: index.php"); exit; break; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Admin Section</title> <link href="adminstyles.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="header"> <?php if(!$username) { ?> <div class="headertext">Admin Section! Please login below</div> </div> <div class="login"> <form action="index.php?action=login" method="post"> <div class="logincredentials">Username: <input type="text" name="username" id="username" /></div> <div class="logincredentials">Password: <input type="password" name="password" id="password" /></div> <input type="submit" value="Login" /> </form> </div> <?php } else { ?> <div class="headertext">Admin Section! Welcome <?php $username; ?></div> </div> <?php } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
radar Posted June 1, 2008 Share Posted June 1, 2008 Ive always had issues with using if (!$username) or something.... I've started doing something like this... if ($_SESSION['username'] == '') { // error need to login.. } else { // logged in... } Also I noticed you missed an echo in your else... should be echo $username, not $username... -- radar Just an edit.. if you use LIKE BINARY such as this: $sql = "SELECT * FROM users WHERE email LIKE BINARY '$formfields[username]' AND password LIKE BINARY '$formfields[password]' AND d_id = '1' AND status = '1'"; it'll make your usernames and passwords case sensative. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted June 1, 2008 Author Share Posted June 1, 2008 ok so the login problem is solved, but the username is not echoing out in the else statement Quote Link to comment Share on other sites More sharing options...
radar Posted June 1, 2008 Share Posted June 1, 2008 try putting this: <?php echo $_SESSION['username']; ?> Also here is some code to put at the bottom of every script you're coding (index.php's or the like).. <?php echo "<pre>SESSION:"; print_r($_SESSION); echo "<br>COOKIE:"; print_r($_COOKIE); echo "<br>POST:"; print_r($_POST); echo "</pre>"; ?> that'll give you a print out of everything you're getting sent to the page you are on.. helps a lot with error problem solving. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted June 1, 2008 Author Share Posted June 1, 2008 thats odd cause it is passing the username to the url but will not print it out Quote Link to comment Share on other sites More sharing options...
radar Posted June 1, 2008 Share Posted June 1, 2008 is it on the net somewhere i can take a look at the output? (if you have to put in a line of code to cover up a password then do)... Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted June 1, 2008 Share Posted June 1, 2008 You probably don't want to write <?php } else { ?> <div class="headertext">Admin Section! Welcome <?php $username; ?></div> </div> <?php } ?> BUT <?php } else { ?> <div class="headertext">Admin Section! Welcome <?php echo $username; ?></div> </div> <?php } ?> you forgot "echo". Quote Link to comment Share on other sites More sharing options...
radar Posted June 1, 2008 Share Posted June 1, 2008 read above I already told him that.. though not sure if he fixed it or not Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted June 2, 2008 Author Share Posted June 2, 2008 ok sorry about not getting back with yall until now, Here is my updated code: <?php require "includes/sql.php"; $action = $_GET['action']; session_start(); ob_start(); $_SESSION['username'] = stripslashes($_POST['username']); $_SESSION['password'] = stripslashes($_POST['password']); $username = $_SESSION['username']; $password = $_SESSION['password']; switch($action) { case "login": $result = mysql_query("SELECT * FROM users where username = '$username' AND password = '$password'") or die("ERROR: " . mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows > 0) { header("location: index.php?username=" . $username . ""); } else { header("location: index.php"); } break; case "logout": session_start(); session_destroy(); ob_end_flush(); header("location: index.php"); exit; break; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Admin Section</title> <link href="adminstyles.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="header"> <?php if($_SESSION['username'] == "") { ?> <div class="headertext">Admin Section! Please login below</div> </div> <div class="login"> <form action="index.php?action=login" method="post"> <div class="logincredentials">Username: <input type="text" name="username" id="username" /></div> <div class="logincredentials">Password: <input type="password" name="password" id="password" /></div> <input type="submit" value="Login" /> </form> </div> <?php } else { ?> <div class="headertext">Admin Section! Welcome <?php echo $_SESSION['username']; ?> <a href="index.php?action=logout">Logout?</a></div> </div> <?php } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
dezkit Posted June 2, 2008 Share Posted June 2, 2008 PROBABLY BECUZ U GOTTA PUT SESSION_START(); AT THE VERY BEGINNING OF THE CODE?!?~~? Quote Link to comment Share on other sites More sharing options...
radar Posted June 2, 2008 Share Posted June 2, 2008 ha i didnt even realize that wasnt there.. goes to show something so simple can not even register with your brain Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted June 2, 2008 Author Share Posted June 2, 2008 nope didn't work! And when i login the Else statement is displayed but the username does not pass through for some reason Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted June 2, 2008 Author Share Posted June 2, 2008 and when you try to click on the logout button nothing happens Quote Link to comment Share on other sites More sharing options...
dezkit Posted June 2, 2008 Share Posted June 2, 2008 100% session problemz Quote Link to comment Share on other sites More sharing options...
dezkit Posted June 2, 2008 Share Posted June 2, 2008 oh wait, try this $result = mysql_query("SELECT * FROM users where username = '$username' AND password = '$password'") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted June 2, 2008 Author Share Posted June 2, 2008 hmmm, i reckon i will contact my hosting company. I tried it and it still didn't work Quote Link to comment Share on other sites More sharing options...
radar Posted June 2, 2008 Share Posted June 2, 2008 just switch to a better hosting company Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted June 2, 2008 Author Share Posted June 2, 2008 LOL yeah tell me about it, or i could host it myself! Quote Link to comment Share on other sites More sharing options...
radar Posted June 2, 2008 Share Posted June 2, 2008 yeah but hosting it yourself gets tedious much better to get a good host... which if interested I know a good one 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.