FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Session not sticking from page to page.





ALostSoul
Code on index.php:

<?php ob_start(); ?>
<?php
$gDate = time();
$gClockShowsSeconds = false;
function getServerDateItems($inDate) {
return date('Y,n,j,G,',$inDate).intval(date('i',$inDate)).','.intval(date('s',$inDate));
}
function clockDateString($inDate) {
return date('l, F j, Y',$inDate);
}
function clockTimeString($inDate, $showSeconds) {
return date($showSeconds ? 'g:i:s' : 'g:i',$inDate).' ';
}
?>
<?php
require "connect.php";

if(isset($_POST['username']) && isset($_POST['password'])) {

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
if(mysql_num_rows($result) > 0) {
session_start();
$_SESSION['username'] = $username;
} else {
echo "<script type='text/javascript'>alert('Wrong username/password! Try again')</script>";
}
}
?>
<html>

<head>
<title>Cyber Infections</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="style/style.css" />
<link rel="stylesheet" type="text/css" href="style/purple.css" />
<meta name="template" content="none">
<script language="JavaScript" type="text/javascript">
<!--
var clockLocalStartTime = new Date();
var clockServerStartTime = new Date(<?php echo(getServerDateItems($gDate))?>);
function clockInit() {
}
//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--
function simpleFindObj(name, inLayer) {
return document[name] || (document.all && document.all[name]) || (document.getElementById && document.getElementById(name)) || (document.layers && inLayer && document.layers[inLayer].document[name]);
}
var clockIncrementMillis = 60000;
var localTime;
var clockOffset;
var clockExpirationLocal;
var clockShowsSeconds = false;
var clockTimerID = null;
function clockInit(localDateObject, serverDateObject)
{
var origRemoteClock = parseInt(clockGetCookieData("remoteClock"));
var origLocalClock = parseInt(clockGetCookieData("localClock"));
var newRemoteClock = serverDateObject.getTime();
var newLocalClock = localDateObject.getTime();
var maxClockAge = 60 * 60 * 1000;
if (newRemoteClock != origRemoteClock) {
document.cookie = "remoteClock=" + newRemoteClock;
document.cookie = "localClock=" + newLocalClock;
clockOffset = newRemoteClock - newLocalClock;
clockExpirationLocal = newLocalClock + maxClockAge;
localTime = newLocalClock;
}
else if (origLocalClock != origLocalClock) {
clockOffset = null;
clockExpirationLocal = null;
}
else {
clockOffset = origRemoteClock - origLocalClock;
clockExpirationLocal = origLocalClock + maxClockAge;
localTime = origLocalClock;
}
var nextDayLocal = (new Date(serverDateObject.getFullYear(),
serverDateObject.getMonth(),
serverDateObject.getDate() + 1)).getTime() - clockOffset;
if (nextDayLocal < clockExpirationLocal) {
clockExpirationLocal = nextDayLocal;
}
}
function clockOnLoad()
{
clockUpdate();
}
function clockOnUnload() {
clockClearTimeout();
}
function clockClearTimeout() {
if (clockTimerID) {
clearTimeout(clockTimerID);
clockTimerID = null;
}
}
function clockToggleSeconds()
{
clockClearTimeout();
if (clockShowsSeconds) {
clockShowsSeconds = false;
clockIncrementMillis = 60000;
}
else {
clockShowsSeconds = true;
clockIncrementMillis = 1000;
}
clockUpdate();
}
function clockTimeString(inHours, inMinutes, inSeconds) {
return inHours == null ? "-:--" : ((inHours == 0 ? "12" : (inHours <= 12 ? inHours : inHours - 12)) + (inMinutes < 10 ? ":0" : ":") + inMinutes + (clockShowsSeconds? ((inSeconds < 10 ? ":0" : ":") + inSeconds) : "") + (inHours < 12 ? " AM" : " PM"));
}
function clockDisplayTime(inHours, inMinutes, inSeconds) {
clockWriteToDiv("ClockTime", clockTimeString(inHours, inMinutes, inSeconds));
}
function clockWriteToDiv(divName, newValue)
{
var divObject = simpleFindObj(divName);
newValue = '<p>' + newValue + '<' + '/p>';
if (divObject && divObject.innerHTML) {
divObject.innerHTML = newValue;
}
else if (divObject && divObject.document) {
divObject.document.writeln(newValue);
divObject.document.close();
}
}
function clockGetCookieData(label) {
var c = document.cookie;
if (c) {
var labelLen = label.length, cEnd = c.length;
while (cEnd > 0) {
var cStart = c.lastIndexOf(';',cEnd-1) + 1;
while (cStart < cEnd && c.charAt(cStart)==" ") cStart++;
if (cStart + labelLen <= cEnd && c.substr(cStart,labelLen) == label) {
if (cStart + labelLen == cEnd) {
return "";
}
else if (c.charAt(cStart+labelLen) == "=") {
return unescape(c.substring(cStart + labelLen + 1,cEnd));
}
}
cEnd = cStart - 1;
}
}
return null;
}
function clockUpdate()
{
var lastLocalTime = localTime;
localTime = (new Date()).getTime();
if (clockOffset == null) {
clockDisplayTime(null, null, null);
}
else if (localTime < lastLocalTime || clockExpirationLocal < localTime) {
document.cookie = 'remoteClock=-';
document.cookie = 'localClock=-';
location.reload();
}
else {
var serverTime = new Date(localTime + clockOffset);
clockDisplayTime(serverTime.getHours(), serverTime.getMinutes(),
serverTime.getSeconds());
clockTimerID = setTimeout("clockUpdate()",
clockIncrementMillis - (serverTime.getTime() % clockIncrementMillis));
}
}
//-->
</script>
<meta-redirect/javascript>
</head>
<body onLoad="clockInit(clockLocalStartTime, clockServerStartTime);clockOnLoad();" onUnload="clockOnUnload()">
<div id="main">
<div id="logo"><h1>Welcome! ( this will be changed later )</h1></div>
<div id="menu">
<ul>
<li><a id="selected" href="http://cyberinfections.frih.net/">home</a></li>
<li><a href="/register.php">Register</a></li>
<li><a href="/upload.php">Upload</a></li>
<li><a href="#">Main Link</a></li>
<li><a href="#">Main Link</a></li>
</ul>
</div>
<div id="content">
<div id="column1">
<div class="sidebaritem">
<h1>Log in!</h1>
<?php
if(isset($_SESSION['username'])){
$username=$_SESSION['username'];
echo "You're logged in as: $username\n\n\n\n";
echo "<a href='index.php?logout=true'>Logout?</a>";
}else{
echo "<form method=\"post\"> Username: <input type=\"text\" name=\"username\"><br>Password: <input type=\"password\" name=\"password\"><br><input type=\"submit\" name=\"login\" value=\"Log In\"></form>\n\n";
echo "<a href=\"register.php\">Register!</a>";
}
if (logout == true) {
unset($_SESSION['username']);
}
?>
</div>
<div class="sidebaritem">
<div class="sbilinks">
<li>
<ul>
<div id="ClockTime" onClick="clockToggleSeconds()">
<?php echo(clockTimeString($gDate,$gClockShowsSeconds));?>
</div>
<div id="ClockBkgnd">
<?php echo(clockDateString($gDate)); ?>
</div>
</ul>
</li>
</div>
</div>
<div class="sidebaritem">
<h1>Staff</h1>
<p>
Admin: Joshua Houde (Admin)<br>
Admin: Caleb Smelser(Smelserdude1)<br>
Admin: Hunter (last name)(hunter612)<br>
Mod/Site Helper: Patrick McKenna(Patrick)
</p>
</div>
</div>
<div id="column2">
<h1>Stuff</h1>
<p>
Content goes here
</p>
</div>
</div>
<div id="footer">
Copyright &copy; 2006 Joshua Houde
</div>
</div>
</body>
</html>
<?php ob_end_flush(); ?>

And on the other pages:

<?php
require "connect.php";
session_start();

if(isset($_POST['username']) && isset($_POST['password'])) {

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
if(mysql_num_rows($result) > 0) {
$_SESSION['username'] = $username;
} else {
echo "<script type='text/javascript'>alert('Wrong username/password! Try again')</script>";
}
}
?>
<?php
session_start();
if(!isset($_SESSION['username'])) {
header("Location:/index.php");
}
?>

That should keep the session there, but it doesn't stay like it should. I've exhausted my brain from thinking straight for answers. Does anyone have any idea why the session isn't staying from page to page?
MrBlueSky
Weird. Works oke if I run it.

--edit--

No, it indeed doesn't keep the session when going to another page. Confused
ALostSoul
MrBlueSky wrote:
Weird. Works oke if I run it.

--edit--

No, it indeed doesn't keep the session when going to another page. Confused



I know, it's making me pissed and sad. I wanna get my user system done before I finish everything else. Like my upload system. I have a mach-up that just uploads to a folder, not a database which is what I'm aiming for next.
Gundamxxg
your code should be this...

Code:

<?php
session_start();
ob_start()'
$gDate = time();
$gClockShowsSeconds = false;
function getServerDateItems($inDate) {
return date('Y,n,j,G,',$inDate).intval(date('i',$inDate)).','.intval(date('s',$inDate));
}
function clockDateString($inDate) {
return date('l, F j, Y',$inDate);
}
function clockTimeString($inDate, $showSeconds) {
return date($showSeconds ? 'g:i:s' : 'g:i',$inDate).' ';

require "connect.php";

if(isset($_POST['username']) && isset($_POST['password'])) {

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
if(mysql_num_rows($result) > 0) {
$_SESSION['username'] = $username;
} else {
echo "<script type='text/javascript'>alert('Wrong username/password! Try again')</script>";
}
}
?>
<html>

<head>
<title>Cyber Infections</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="style/style.css" />
<link rel="stylesheet" type="text/css" href="style/purple.css" />
<meta name="template" content="none">
<script language="JavaScript" type="text/javascript">
<!--
var clockLocalStartTime = new Date();
var clockServerStartTime = new Date(<?php echo(getServerDateItems($gDate))?>);
function clockInit() {
}
//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--
function simpleFindObj(name, inLayer) {
return document[name] || (document.all && document.all[name]) || (document.getElementById && document.getElementById(name)) || (document.layers && inLayer && document.layers[inLayer].document[name]);
}
var clockIncrementMillis = 60000;
var localTime;
var clockOffset;
var clockExpirationLocal;
var clockShowsSeconds = false;
var clockTimerID = null;
function clockInit(localDateObject, serverDateObject)
{
var origRemoteClock = parseInt(clockGetCookieData("remoteClock"));
var origLocalClock = parseInt(clockGetCookieData("localClock"));
var newRemoteClock = serverDateObject.getTime();
var newLocalClock = localDateObject.getTime();
var maxClockAge = 60 * 60 * 1000;
if (newRemoteClock != origRemoteClock) {
document.cookie = "remoteClock=" + newRemoteClock;
document.cookie = "localClock=" + newLocalClock;
clockOffset = newRemoteClock - newLocalClock;
clockExpirationLocal = newLocalClock + maxClockAge;
localTime = newLocalClock;
}
else if (origLocalClock != origLocalClock) {
clockOffset = null;
clockExpirationLocal = null;
}
else {
clockOffset = origRemoteClock - origLocalClock;
clockExpirationLocal = origLocalClock + maxClockAge;
localTime = origLocalClock;
}
var nextDayLocal = (new Date(serverDateObject.getFullYear(),
serverDateObject.getMonth(),
serverDateObject.getDate() + 1)).getTime() - clockOffset;
if (nextDayLocal < clockExpirationLocal) {
clockExpirationLocal = nextDayLocal;
}
}
function clockOnLoad()
{
clockUpdate();
}
function clockOnUnload() {
clockClearTimeout();
}
function clockClearTimeout() {
if (clockTimerID) {
clearTimeout(clockTimerID);
clockTimerID = null;
}
}
function clockToggleSeconds()
{
clockClearTimeout();
if (clockShowsSeconds) {
clockShowsSeconds = false;
clockIncrementMillis = 60000;
}
else {
clockShowsSeconds = true;
clockIncrementMillis = 1000;
}
clockUpdate();
}
function clockTimeString(inHours, inMinutes, inSeconds) {
return inHours == null ? "-:--" : ((inHours == 0 ? "12" : (inHours <= 12 ? inHours : inHours - 12)) + (inMinutes < 10 ? ":0" : ":") + inMinutes + (clockShowsSeconds? ((inSeconds < 10 ? ":0" : ":") + inSeconds) : "") + (inHours < 12 ? " AM" : " PM"));
}
function clockDisplayTime(inHours, inMinutes, inSeconds) {
clockWriteToDiv("ClockTime", clockTimeString(inHours, inMinutes, inSeconds));
}
function clockWriteToDiv(divName, newValue)
{
var divObject = simpleFindObj(divName);
newValue = '<p>' + newValue + '<' + '/p>';
if (divObject && divObject.innerHTML) {
divObject.innerHTML = newValue;
}
else if (divObject && divObject.document) {
divObject.document.writeln(newValue);
divObject.document.close();
}
}
function clockGetCookieData(label) {
var c = document.cookie;
if (c) {
var labelLen = label.length, cEnd = c.length;
while (cEnd > 0) {
var cStart = c.lastIndexOf(';',cEnd-1) + 1;
while (cStart < cEnd && c.charAt(cStart)==" ") cStart++;
if (cStart + labelLen <= cEnd && c.substr(cStart,labelLen) == label) {
if (cStart + labelLen == cEnd) {
return "";
}
else if (c.charAt(cStart+labelLen) == "=") {
return unescape(c.substring(cStart + labelLen + 1,cEnd));
}
}
cEnd = cStart - 1;
}
}
return null;
}
function clockUpdate()
{
var lastLocalTime = localTime;
localTime = (new Date()).getTime();
if (clockOffset == null) {
clockDisplayTime(null, null, null);
}
else if (localTime < lastLocalTime || clockExpirationLocal < localTime) {
document.cookie = 'remoteClock=-';
document.cookie = 'localClock=-';
location.reload();
}
else {
var serverTime = new Date(localTime + clockOffset);
clockDisplayTime(serverTime.getHours(), serverTime.getMinutes(),
serverTime.getSeconds());
clockTimerID = setTimeout("clockUpdate()",
clockIncrementMillis - (serverTime.getTime() % clockIncrementMillis));
}
}
//-->
</script>
<meta-redirect/javascript>
</head>
<body onLoad="clockInit(clockLocalStartTime, clockServerStartTime);clockOnLoad();" onUnload="clockOnUnload()">
<div id="main">
<div id="logo"><h1>Welcome! ( this will be changed later )</h1></div>
<div id="menu">
<ul>
<li><a id="selected" href="http://cyberinfections.frih.net/">home</a></li>
<li><a href="/register.php">Register</a></li>
<li><a href="/upload.php">Upload</a></li>
<li><a href="#">Main Link</a></li>
<li><a href="#">Main Link</a></li>
</ul>
</div>
<div id="content">
<div id="column1">
<div class="sidebaritem">
<h1>Log in!</h1>
<?php
if(isset($_SESSION['username'])){
$username=$_SESSION['username'];
echo "You're logged in as: $username\n\n\n\n";
echo "<a href='index.php?logout=true'>Logout?</a>";
}else{
echo "<form method=\"post\"> Username: <input type=\"text\" name=\"username\"><br>Password: <input type=\"password\" name=\"password\"><br><input type=\"submit\" name=\"login\" value=\"Log In\"></form>\n\n";
echo "<a href=\"register.php\">Register!</a>";
}
$logout = $_GET['logout'];
if ($logout == true) {
unset($_SESSION['username']);
}
?>
</div>
<div class="sidebaritem">
<div class="sbilinks">
<li>
<ul>
<div id="ClockTime" onClick="clockToggleSeconds()">
<?php echo(clockTimeString($gDate,$gClockShowsSeconds));?>
</div>
<div id="ClockBkgnd">
<?php echo(clockDateString($gDate)); ?>
</div>
</ul>
</li>
</div>
</div>
<div class="sidebaritem">
<h1>Staff</h1>
<p>
Admin: Joshua Houde (Admin)<br>
Admin: Caleb Smelser(Smelserdude1)<br>
Admin: Hunter (last name)(hunter612)<br>
Mod/Site Helper: Patrick McKenna(Patrick)
</p>
</div>
</div>
<div id="column2">
<h1>Stuff</h1>
<p>
Content goes here
</p>
</div>
</div>
<div id="footer">
Copyright &copy; 2006 Joshua Houde
</div>
</div>
</body>
</html>
<?php ob_end_flush(); ?>

And on the other pages:

<?php
require "connect.php";
session_start();

if(isset($_POST['username']) && isset($_POST['password'])) {

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
if(mysql_num_rows($result) > 0) {
$_SESSION['username'] = $username;
} else {
echo "<script type='text/javascript'>alert('Wrong username/password! Try again')</script>";
}
}
?>
<?php
session_start();
if(!isset($_SESSION['username'])) {
header("Location:/index.php");
}
?>



Your main problem was the fact that you need to have the session_start(); only once per page and that one time needs to be at the very very top. also if you are going to have redirects then you need to have those happen before the first html tag to be displayed otherwise you will get errors.
Biggest thing to remember is you only need session_start() once and that needs to be at the very very beginning of the document.
thinkingskull
I think adding session_start(); before headers r sent (i.e befor u print somethin on screen) will solve d problem.....
gerpg
Session_start(); may only solve some problems however if you want to set a cookie on the local machine of the user on your webiste i suggest the session_register(); function this will set a cookie on the local machine which you can request each time a page loads.

eg. this will set the cookie.
Code:

<?
session_start();

$name = "bob";

session_register('$name');

?>


This must be done before any HTML is echoed or displayed on your page or you will get errors.

If you wish to destroy all cookies at a certain point e.g. a 'logout' function use...
Code:

Session_destroy();


Hope this helps,

Louis.
sonam
session_start(); must come before anything what browser can detect. Even more, some space before <?php can also to cause problems.

Sonam
Related topics
htaccess Password Protection
Traduire Frihost en francais et allemand (La page d´accuei)
[tutor] How to protect images without htaccess using PHP
Led Zeppelin+the old stuff
session variables
pasword gate
Session variables versus page variables
What is your browsers start page?
session not working behind router
Session problem
firefox 3 beta
MySQL - Session error?
User Authentication based Menu Panel
Session issue. Please Help.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.