dennismonsewicz Posted January 5, 2009 Share Posted January 5, 2009 I was able to fix a problem with a previous post that I was having with my code not logging in properly but now the code is logging in but once you go to a secondary page the login form reappears and you lose all of your options <?php require "includes/sql.php"; $error = $_GET['error']; switch($error) { case "username": $u_error = '<span style="color: red; display: block; font-size: 11px; padding: 5px; font-weight: bold">Username Invalid!</span>'; break; case "password": $p_error = '<span style="color: red; display: block; font-size: 11px; padding: 5px; font-weight: bold">Password Incorrect!</span>'; break; case "both": $b_error = '<span style="color: red; display: block; font-size: 11px; padding: 5px; font-weight: bold">An Error Occurred!</span>'; break; } $action = $_GET['action']; switch($action) { case "logout": session_destroy(); ob_end_flush(); header("location:index.php"); break; } if(isset($_POST['login_submit'])) { if(!empty($_POST['username'])) { $tmpUsername = stripslashes($_POST['username']); } else { header("location: index.php?error=username"); } if(!empty($_POST['password'])) { $tmpPassword = stripslashes($_POST['password']); } else { header("location: index.php?error=password"); } if(empty($_POST['password']) && empty($_POST['username'])) { header("location: index.php?error=both"); } $login_ck_query = mysql_query("SELECT * FROM users WHERE username = '" . mysql_real_escape_string($tmpUsername) . "' AND password = '" . mysql_real_escape_string($tmpPassword) . "'")or die(mysql_error()); $num_rows = mysql_num_rows($login_ck_query); if($num_rows > 0) { $_SESSION['username'] = $tmpUsername; $query = mysql_query("SELECT * FROM devotionals")or die(mysql_error()); while($results = mysql_fetch_object($query)) { $writer = $results->writer; $publish = $results->publish; if($publish == 0) { $published = '<div class="publish-no">Not Approved!</div>'; } $devotional_query .= '<li><a href="devotional.php?action=view&id=' . $results->devotional_id . '&writer=' . $writer . '">' . ucwords(strtolower($results->devotional_title)) . '</a>' . $published . '</li>'; } if($_SESSION) { echo '<div class="accordion">'; echo '<a class="title">My Profile</a>'; echo '<div>'; echo '<ul>'; echo '<li><a href="index.php">Home</a></li>'; echo '<li><a href="profile.php?tool=changepw">Change Password</a></li>'; echo '<li><a href="index.php?action=logout">Logout</a></li>'; echo '</ul>'; echo '</div>'; echo '<a class="title">Tools</a>'; echo '<div>'; echo '<ul>'; echo '<li><a href="devotional.php?action=create">Create a Devotional</a></li>'; echo '<li><a href="devotional.php?action=edit_list">Edit a Devotional</a></li>'; echo '<li><a href="profile.php?tool=addfriend">Signup A Friend</a></li>'; echo '<li><a href="email.php?action=emailafriend">Email A Friend</a></li>'; echo '</ul>'; echo '</div>'; echo '<a class="title">Devotionals</a>'; echo '<div>'; echo '<ul>'; echo $devotional_query; echo '</ul>'; echo '</div>'; echo '<a class="title">Topics</a>'; echo '<div>'; echo '<ul>'; echo '<li><a href="submit.php?action=addtopic">Submit a Topic</a></li>'; echo '</ul>'; echo '</div>'; echo '</div>'; //div closes accordion div } } } else { echo '<div class="accordion">'; echo '<a class="title">Login</a>'; echo '<div>'; echo '<form action="index.php" method="post" name="login_form">'; echo $b_error; echo '<label>Username:</label> <input type="text" name="username" id="username" />'; echo $u_error; echo '<label>Password: </label> <input type="password" name="password" id="password" />'; echo $p_error; echo '<input type="submit" id="submit" name="login_submit" value="" />'; echo '</form>'; echo '</div>'; echo '</div>'; //closes accordion div } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/ Share on other sites More sharing options...
dennismonsewicz Posted January 5, 2009 Author Share Posted January 5, 2009 any ideas on this? Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/#findComment-730263 Share on other sites More sharing options...
KevinM1 Posted January 5, 2009 Share Posted January 5, 2009 any ideas on this? Where's session_start()? Remember, you need to put that function at the beginning of every file you want to have session access. If those options are dependent on the session data, they won't show unless you have session_start() invoked. Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/#findComment-730271 Share on other sites More sharing options...
dennismonsewicz Posted January 5, 2009 Author Share Posted January 5, 2009 i have it on the header.php file which is included on every single file... is this bad practice? Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/#findComment-730274 Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 i have it on the header.php file which is included on every single file... is this bad practice? Nope, good practice, no need to duplicate code when you can add it in one file and include it with other code that each page uses. I would however suggest: if($_SESSION) { To be a unique identifier such as "loggedin" if($_SESSION['loggedin']) { Just make sure to set that variable to be true when a user logs in. Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/#findComment-730276 Share on other sites More sharing options...
KevinM1 Posted January 5, 2009 Share Posted January 5, 2009 i have it on the header.php file which is included on every single file... is this bad practice? Nope, actually the opposite. So long as the header file is included in the pages that need session access, you're fine. Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/#findComment-730277 Share on other sites More sharing options...
dennismonsewicz Posted January 5, 2009 Author Share Posted January 5, 2009 gotcha... well the header.php file is included on every page that Sessions are needed... so i have no idea why this is not working lol.. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/#findComment-730279 Share on other sites More sharing options...
KevinM1 Posted January 5, 2009 Share Posted January 5, 2009 You should double-check your conditionals, and make sure they're all lined up, so-to-speak. Make sure that when a valid login occurs, the process doesn't continue to show the login form. To help you in this, you might want to comment out all of the code not related to an error state. That way you know you're just dealing with the simplest case - a legit user. Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/#findComment-730281 Share on other sites More sharing options...
dennismonsewicz Posted January 5, 2009 Author Share Posted January 5, 2009 I loaded some older code for this page to get it back to working for now.. its not a live site yet so no biggy... Since this script is working I am going to update some of the code to meet some of my current coding standards and go from there. Thanks for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/139583-solved-losing-login-options-after-going-to-secondary-pages/#findComment-730286 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.