Akenatehm Posted March 1, 2009 Share Posted March 1, 2009 Hey Guys, This script doesn't seem to be creating and checking sessions properly. When I click submit, it just resets the page. Heres the functions I'm calling from functions.php: function startusersession() { session_start(); $_SESSION["loggedin"]; } function endsession() { foreach($_SESSION as $key => $val) unset($_SESSION[$key]); } function checksession() { if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']) { header("Location:alreadyloggedin.php"); } } and here's login.php <?php include 'functions.php'; include 'connect.php'; checksession(); if (isset($_POST['submit'])) { if($_POST['password'] != $_POST['confirmpassword']) { $result = '<font color="red">Passwords Do Not Match</font>'; } else{ $checkuserexists = mysql_query("SELECT * FROM accounts WHERE username = '$username'"); if(mysql_num_rows($checkuserexists) > 0) { $result = '<font color="red">User Does Not Exist</font>'; } else { $usersdetails = mysql_fetch_assoc($checkuserexists); $usersusername = $usersdetails['username']; $userspassword = $usersdetails['password']; $formusername = $_POST['username']; $formpass = $_POST ['password']; if($formusername != $usersusername || $formpass != $userspassword) { $result = "Incorrect Username or Password"; } else { startusersession(); headers("Location:loggedin.php"); } } } } ?> <html> <head> <title>Login - Fall of The Empire</title> <link rel="stylesheet" href="styles.css" type="text/css"/> </head> <body bgcolor="272d31"> <div align="center" class="header"> <img src="images/logo.png" width="473" height="227" alt="Logo"> </div> <br> <center> <div class="main"> <div class="navbar"> <img src="images/nav_top.png" width="232" height="46" alt="Nav Top"><br> <a href="index.php"><img src="images/nav_home.png" width="232" height="33" alt="Nav Home" border="0"></a><br> <a href="news.php"><img src="images/nav_news.png" width="232" height="32" alt="Nav News" border="0"></a><br> <a href="about.php"><img src="images/nav_about.png" width="232" height="23" alt="Nav About" border="0"></a><br> <a href="login.php"><img src="images/nav_login.png" width="232" height="32" alt="Nav Login" border="0"></a><br> <a href="register.php"><img src="images/nav_register.png" width="232" height="25" alt="Nav Register" border="0"></a><br> <a href="contactus.php"><img src="images/nav_bottom.png" width="232" height="179" alt="Nav Bottom" border="0"><br></a> </div> <div class="cb"> <img src="images/login.png" align="right" class="welcome"> <br> <br> <br> <br> <table width="300px" align="right"> <form action="<?php echo $PHP_SELF; ?>" method="POST"> <tr> <td> <font color="white"> Username: </font> </td> <td> <input type="text" name="username"> </td> </tr> <tr> <td> <font color="white"> Password: </font> </td> <td> <input type="password" name="password"> </td> </tr> <tr> <td> <br> </td> <td> </td> <tr> <td> </td> <td align="right" style="padding-right:8px"> <input type="submit" name="submit" value="Login"align="right"> </td> </tr> </form> </table> <p><?php echo $error; ?></p> </div> </div> </center> </body> </html> Help would be great! Quote Link to comment https://forums.phpfreaks.com/topic/147373-solved-sessions/ Share on other sites More sharing options...
RussellReal Posted March 1, 2009 Share Posted March 1, 2009 hello again bro try doing session_start() before you include the files.. then just use $_SESSION inside the functions because in order to fill the $_SESSION superglobal you need to actually start the session otherwise $_SESSION will not be present.. Quote Link to comment https://forums.phpfreaks.com/topic/147373-solved-sessions/#findComment-773602 Share on other sites More sharing options...
Akenatehm Posted March 1, 2009 Author Share Posted March 1, 2009 Hey Lol. I found out their were a few reasons why it wasn't working. Firstly, I didn't set the variable $username to be checked before it was checked. Secondly, I had set a 20 char limit in the table to the password field so it was only accepting 20 of the should be 32 md5 pass chars ther shud have been. After fixing, it is algud. Quote Link to comment https://forums.phpfreaks.com/topic/147373-solved-sessions/#findComment-773609 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.