Jump to content

Sessions Playing Up.


Flukey

Recommended Posts

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 inputted
if (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;">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.