Unholy Prayer Posted March 21, 2007 Share Posted March 21, 2007 I still can't get my login script to work. I login and I get no errors but I have a code place to display a welcome message when the user is logged in but display the login form if they are logged out. I'm not sure which script is wrong. This is my login script: <?php ob_start(); require_once('config.php'); include('constants.php'); if(isset($_SESSION['username']) && isset($_SESSION['password'])) { //REDIRECT TO USERS PROFILE... header("Location: http://$domain/voltbb"); } //end if logged in $username = $_POST['username']; $password = md5($_POST['password']); //IF SUBMIT BUTTON PRESSED if(isset($_POST['submit'])) { if(!$_POST['username']) die("You did not enter a username. Please go back and provide your account username."); if(!$_POST['password']) die("You need to enter your password before you can login. Please go back and do so."); //set cookie if checked if(!empty($_POST['stay_in'])) { $joined =''.$_POST['username'].'[]'.md5($_POST['password']).''; setcookie('login_cookie', $joined, 2147483647, '/', '.mtechdev.com'); } //end if //verify user... $get_user = mysql_query("SELECT * FROM `members` WHERE username = '$username' AND password = '$password'"); $q = mysql_fetch_object($get_user); if(!$q) die("An error occured, please verify your username and password are correct."); //set session variables $_SESSION['is_online'] = 1; $_SESSION['username'] = $username; $_SESSION['password'] = $password; session_write_close(); header("Location: http://$domain/voltbb"); } else { //show login form ?> <form name="login" method="post" action="<? $_SERVER['PHP_SELF']; ?>"> <table align='center'cellspacing='1' cellpadding='1' border='0'> <tr> <td align='center' colspan='2' class='ctop'>Member Login</td> </tr><tr> <td class='inputrow' align='right'>Username: </td> <td align='left' class='inputrow'><input type="text" id="username" name="username"></td> </tr> <tr> <td class='inputrow' align='right'>Password:<input type="password" id="password" name="password"></td> </tr> <tr> <td align='center' class='inputrow' colspan='2'><input type="submit" value="Submit" name="submit" id="submit"></td> </tr> <tr> <td align='center' class='inputrow' colspan='2'>Remember? <input type="checkbox" name="stay_in[]" checked="yes"></td> </tr> </table> </form> <? }//end else ?> This is my page header script: ob_start(); session_start( ); require_once('config.php'); //check cookie if ($_SESSION['logged_in'] != 1 && isset($_COOKIE['login_cookie'])) { list($username, $password) = explode('[]', $_COOKIE['login_cookie']); $query = mysql_query("SELECT `password` FROM `members` WHERE `username` = '$username'"); if (mysql_num_rows($query) == 1) { $password = mysql_fetch_array($query); if ($password->password == $password) { $_SESSION['logged_in'] = 1; $_SESSION['username'] = $username; $_SESSION['password'] = md5($password); } } } if(!isset($_SESSION['username']) && !isset($_SESSION['password'])) { $_SESSION['logged_in'] = 0; $user = "Guest"; } Link to comment https://forums.phpfreaks.com/topic/43600-more-help/ Share on other sites More sharing options...
btherl Posted March 21, 2007 Share Posted March 21, 2007 Could you explain more clearly what isn't working? Link to comment https://forums.phpfreaks.com/topic/43600-more-help/#findComment-211767 Share on other sites More sharing options...
Iceman512 Posted March 21, 2007 Share Posted March 21, 2007 Hi, You seem quite adept at php coding, so you must already know this. Anyway, without sifting through your variables, you could use the following simple solution and replace your session variables and data accordingly: <?php if ((isset($_SESSION['username'])) && (isset($_SESSION['password']))){ echo '<br />Welcome to mySite, '.$_SESSION['username'].'<br />.'; echo '<a href="logout.php">logout</a><br />'; } else { echo 'Please login:'; echo '<form action="login.php" method="post">'; echo 'Username:<br />'; echo '<input type="text" name="username" />'; echo 'Password:<br />'; echo '<input type="text" name="password" />'; echo '</form>'; } ?> Just drop this in where you want the condition to appear. Hope that helps, Iceman Link to comment https://forums.phpfreaks.com/topic/43600-more-help/#findComment-211879 Share on other sites More sharing options...
Unholy Prayer Posted March 21, 2007 Author Share Posted March 21, 2007 I think it's the login script because when I login it still displays the form and I added the code you just posted. Link to comment https://forums.phpfreaks.com/topic/43600-more-help/#findComment-212307 Share on other sites More sharing options...
Unholy Prayer Posted March 21, 2007 Author Share Posted March 21, 2007 Can anyone help? Link to comment https://forums.phpfreaks.com/topic/43600-more-help/#findComment-212369 Share on other sites More sharing options...
shaunrigby Posted March 21, 2007 Share Posted March 21, 2007 Are your session vars actually registering? if not it is a config problem in ur php.ini Link to comment https://forums.phpfreaks.com/topic/43600-more-help/#findComment-212390 Share on other sites More sharing options...
Unholy Prayer Posted March 21, 2007 Author Share Posted March 21, 2007 I have no idea. How do I change that? Link to comment https://forums.phpfreaks.com/topic/43600-more-help/#findComment-212430 Share on other sites More sharing options...
shaunrigby Posted March 21, 2007 Share Posted March 21, 2007 page1.php At the very top of the page (Above everything), type <?php session_start(); ?> in the body section type: <?php $_SESSION['test'] = "Hello World!"; ?> <a href="page2.php">Next Page</a> page2.php At the very top of the page (Above everything), type <?php session_start(); ?> in the body section type: <?php print $_SESSION['test']; When you visit page2.php you should see Hello World printed if your sessions are registering successfully... Link to comment https://forums.phpfreaks.com/topic/43600-more-help/#findComment-212434 Share on other sites More sharing options...
Unholy Prayer Posted March 22, 2007 Author Share Posted March 22, 2007 Ok, Hello World displayed when I uploaded those files and visited them, so they are registering. It must be something with my code but I can't figure out why. Link to comment https://forums.phpfreaks.com/topic/43600-more-help/#findComment-212472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.