Noskiw Posted February 23, 2012 Share Posted February 23, 2012 I've decided to "re-learn" PHP. I've created a registration script which works well, however, the login script is not going to plan for me. This is the script: <?php include "./global/global.php"; ?> <?php error_reporting (E_ALL ^ E_NOTICE); ?> <?php $username = $_POST['username']; $password = $_POST['password']; $submit = $_POST['submit']; if($submit){ $sql1 = "SELECT * FROM test2 WHERE username = '".$username."'"; $res1 = mysql_query($sql1) or die(mysql_error()); $numrows = mysql_num_rows($res1); if($numrows != 0){ while($row = mysql_fetch_assoc($res1)){ $dbusername = $row['username']; $dbpassword = $row['password']; } if($username==$dbusername && $password==$dbpassword){ ob_start(); $_SESSION['username']=$username; header("Location: index.php"); } }else{ echo("That user doesn't exist!"); } }else{ ?> <form action="login.php" method="POST"> <input type="text" name="username" /> <input type="password" name="password" /> <input type="submit" name="submit" value="Login" /> </form> <?php } ?> The problem is that it isn't building the session, so when I go back to the homepage, which looks like this: <?php include "./global/global.php"; session_start(); ?> <?php error_reporting (E_ALL ^ E_NOTICE); ?> <html> <?php if($_SESSION){ echo("Welcome, ".$_SESSION['username']); }else{ echo("Please login <a href='login.php'>here</a>"); } ?> </html> It shows that I should login. I can't seem to see what's wrong because I have a script very similar to it and it works fine, so if anyone could help, I'd appreciate it. Link to comment https://forums.phpfreaks.com/topic/257624-trying-to-create-a-login-script-from-scratch/ Share on other sites More sharing options...
trq Posted February 23, 2012 Share Posted February 23, 2012 You might want to let us know what the actual issue is. Link to comment https://forums.phpfreaks.com/topic/257624-trying-to-create-a-login-script-from-scratch/#findComment-1320412 Share on other sites More sharing options...
Noskiw Posted February 23, 2012 Author Share Posted February 23, 2012 Whoops, I meant to also say that it's not building a session when I login and it takes me back to the home page. Link to comment https://forums.phpfreaks.com/topic/257624-trying-to-create-a-login-script-from-scratch/#findComment-1320416 Share on other sites More sharing options...
litebearer Posted February 23, 2012 Share Posted February 23, 2012 does "global.php' have session_start() at the top? Link to comment https://forums.phpfreaks.com/topic/257624-trying-to-create-a-login-script-from-scratch/#findComment-1320424 Share on other sites More sharing options...
Noskiw Posted February 23, 2012 Author Share Posted February 23, 2012 I did try that. I swear, but it works now, I've just had to remove session_start() from index.php to get it to work properly. Thanks for your help. :') Link to comment https://forums.phpfreaks.com/topic/257624-trying-to-create-a-login-script-from-scratch/#findComment-1320431 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.