Flukey Posted November 7, 2006 Share Posted November 7, 2006 Hi guys, I'm using sessions for the user_id and user_name of a user when they log in. However, it works fine when i log in, the sessions get saved and it says "Hello Jamie", "Logged in at: 14:18" etc.... But as soon as i refresh the page, the sessions get destroyed ??? Very odd. I can't work it out yet. Any help would be hugely appreciated![code=php:0]<?php// Log user out.if ($_GET["logout"] == 1) { session_destroy();}// check for a login attempt$logon_failed = 0;// Check if the fields have been inputtedif (isset($_POST['usr'])) { // check for mysql injection $usr = cleanSQL($_POST["usr"]); $pwd = cleanSQL($_POST["pwd"]); // check db $sql = "SELECT user_id, user_name FROM tbl_user WHERE user_name = '".$usr."' AND user_password = '".$pwd."';"; // perform query $rs = mysql_query($sql,$conn); // fetch user details if (mysql_num_rows($rs) == 1) { // Username and password ok, set sessions $result = mysql_fetch_array($rs); $_SESSION["user_id"] = $result["user_id"]; $_SESSION['date_logged_in'] = date("H:i:s"); $_SESSION["user_name"] = $result["user_name"]; } else { $logon_failed = 1; }}//?><body> <div id="banner"> <div style="float:left; padding-left: 10px;"> <img src="/images/70x70.gif"> </div> <div style="float:right; padding-right: 10px;"> </div> </div> <div id="navbar"> <?php if (!isset($_SESSION["user_id"])) { ?><a href="index.php">Home</a> | <a href ="about.php">About the site</a> <?php } else { ?><a href="index.php">My Second Edition</a> | <a href='usercp.php'>Change my details</a> | <a href='/publish.php'>Publish an article</a> | <a href ="/about.php">About the site</a> | <a href ="/index.php?logout=true">Log me out</a> <?php } ?> </div> <div id="date"><?echo date("l F jS Y");?></div> <div id="sidebar"> <div id="login"> <?php if (!isset($_SESSION['user_id'])) { ?> <form action="index.php" method="POST"> <span style="color: #336699; font-weight: bold; font-family: 'Trebuchet MS'; font-size: 14pt;">Login</span> <?if ($logon_failed == 1) { echo "<br /><span class='invalid'>Login failed!</span> <br />"; }?> <span style="color:#4684C1;">Username:</span> <br /> <input type="text" name="usr" id="usr" style="width: 85%;"><br /> <span style="color:#4684C1;">Password:</span> <br /> <input type="password" name="pwd" id="pwd" style="width: 85%;"><br /> <input style="color:#336699; border: 1px solid #000099; background-color: #fff; font-size: 10pt; font-weight: bold; font-family: 'Trebuchet MS';" type="submit" name="login" value="login"><br /> <a class="register" href="register.php">I'm a new user</a> </form> <?php } elseif(isset($_SESSION['user_id'])) { echo('<p style=\'color: #000; font-weight: normal; font-size: 10pt;\'>Logged in as: <b>' . $_SESSION["user_name"].'</b><br />'); echo 'Logged in at: <br /><b>' . $_SESSION['date_logged_in'] . '</b>'; } ?> </div> <!--<div id="rss"> RSS feeds <br /> <img src="/images/xml.gif"> </div>--> <div id="sidebar_nav"> <ul> <li><a href ='index.php'>Top Stories</a></li> <?php// lookup genres in db$sql="SELECT genre_id, genre_name FROM tbl_genre ORDER BY genre_name;";$rs=mysql_query($sql, $conn); echo "<ul>";while($results = mysql_fetch_assoc($rs)) { $genre_name = $results["genre_name"]; $genre_id = $results["genre_id"]; echo("<li><a href ='index.php?genre_id=$genre_id'>$genre_name</a></li>");} ?> </ul> </div> </div> <div id="maincontent">[/code]This is a header file for the template. :)Thanks guys Link to comment https://forums.phpfreaks.com/topic/26446-sessions-playing-up/ Share on other sites More sharing options...
onlyican Posted November 7, 2006 Share Posted November 7, 2006 you need to have <?phpsession_start(); ?>on line 1 of your code (BEFORE any other code, including the html tags) Link to comment https://forums.phpfreaks.com/topic/26446-sessions-playing-up/#findComment-120937 Share on other sites More sharing options...
Flukey Posted November 7, 2006 Author Share Posted November 7, 2006 hi,thats on the global.php page.So for example on index.php i have:[code=php:0]include("inc/global.php");include("inc/header.php");include("inc/footer.php");[/code]My apologies though, i forgot to say. Link to comment https://forums.phpfreaks.com/topic/26446-sessions-playing-up/#findComment-120939 Share on other sites More sharing options...
onlyican Posted November 7, 2006 Share Posted November 7, 2006 ok, but where are the includes in that code? Link to comment https://forums.phpfreaks.com/topic/26446-sessions-playing-up/#findComment-120954 Share on other sites More sharing options...
Flukey Posted November 7, 2006 Author Share Posted November 7, 2006 thats the header.php page.Its the index page which includes, global.php, header.php and footer.php Link to comment https://forums.phpfreaks.com/topic/26446-sessions-playing-up/#findComment-120957 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.