sessions og php.
Hej.Jeg har bygget min side op vha. switch og include.
Jeg vil, før jeg beskriver mit problem, beskrive strukturen af siden.
Koden til de forskellige sider, viser jeg længere nede.
Struktur:
index.php
[leftmenu.php]
[main.php]
[rightmenu.php]
leftmenu.php
[der er ikke noget inkluderet, det er bare min menu.]
main.php
[Her kører min switch. Det er her diverse sider bliver inkluderet, når de via switch bliver kaldt.]
rightmenu.php
[login.php](Hvis man ikke er logget ind)
[community.php] (Skulle vises hvis man er logget ind)
[Stats](diverse statistikker)
Håber det gav lidt overblik...
Problem:
Det handler her om rightmenu.php som skal vise login.php, hvis man ikke er logget ind. Og community.php hvis man er logget ind.
Men, når jeg logger ind, sker der det. Rightmenu.php inkludere ganske rigtigt community.php, lige når jeg logger ind. Trykker jeg herefter på et andet link i leftmenu.php så går den tilbage til login.php, Altså sessionen er ikke aktiv mere. Jeg aner ikke hvorfor.
Nu vil jeg vise min kode, til dem der er kommet så langt.
Kode:
INDEX.PHP
[start kode]
<?
$StartTimer = GetMicroTime();
function GetMicroTime(){
list($ms, $s) = explode(" ",microtime());
return ((float)$ms + (float)$s);
}
?>
<html>
<head>
<title>Fjappe.dk - Inden ide ;(</title>
<link rel="STYLESHEET" href="css/styles.css" type="text/css">
</head>
<body bottommargin="0" topmargin="0" bgcolor="#DADBDA">
<table width="970" height="100%" cellpadding="0" cellspacing="0" align="center">
<tr>
<td valign="top" class="siteborder">
<!-- toppen -->
<table width="970" cellpadding="0" cellspacing="0" align="center">
<tr>
<td valign="top">
<img src="img/top01.gif" width="970" height="16" alt="" border="0"><br>
<a href="http://www.fjappe.dk"><img src="img/top02.gif" width="970" height="101" alt="" border="0"></a><br>
<img src="img/top03.gif" width="970" height="20" alt="" border="0"><br>
</tr>
</table>
<!-- Toppen Slut -->
<!-- top bars(menu, main, misc) -->
<table width="970" cellpadding="0" cellspacing="0" align="center">
<tr>
<td valign="top"><img src="img/menu_bar.gif" width="168" height="23" alt="" border="0"></td>
<td valign="top" background="img/main_bar.gif"><img src="img/main_bar.gif" width="635" height="23" alt="" border="0"></td>
<td valign="top"><img src="img/misc_bar.gif" width="168" height="23" alt="" border="0"></td>
</tr>
</table>
<!-- Top bars slut -->
<!-- main tables(menu, main, misc) -->
<table height="100%" width="970" cellpadding="0" cellspacing="0" align="center">
<tr>
<td class="text" bgcolor="#FFFFFF" valign="top" height="100%" width="164">
<!-- Left menu -->
<?php include('inc/leftmenu/leftmenu.php') ?>
<!-- Left menu slut -->
<td valign="top" height="100%" width="9" background="img/right_borders.gif"></td>
<td class="text" bgcolor="#FFFFFF" valign="top" height="100%" width="623">
<!-- Main on site-->
<br>
<?php include('inc/main/main.php') ?>
<!-- Main on site slut-->
</td>
<td valign="top" height="100%" width="9" background="img/left_borders.gif"></td>
<td class="text" bgcolor="#FFFFFF" valign="top" height="100%" width="163">
<!-- Right Menu -->
<?
$StopTimer = GetMicroTime();
$CPUtime = $StopTimer - $StartTimer;
?>
<?php include('inc/rightmenu/rightmenu.php') ?>
</td>
</tr>
</table>
</body>
</html>
[slut kode]
LEFTMENU.PHP
[start kode]
<!-- Nyheds menusektionen -->
<br>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<div onclick="location.href='?mode=allnews'" onmouseover="style.cursor='hand';">
<table border="0" width="164" bgcolor="#D1DBE3" bordercolor="#D1DBE3">
<tr>
<td class="text" width="100%">
<strong> Nyheder</strong>
</td>
</tr>
</table>
</div>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';" onclick="location.href='?mode=news&kat=it'">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
IT
</td>
</tr>
</table>
</div>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';" onclick="location.href='?mode=news&kat=film'">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Film
</td>
</tr>
</table>
</div>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';" onclick="location.href='?mode=news&kat=generelt'">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Generelt
</td>
</tr>
</table>
</div>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';" onclick="location.href='?mode=news&kat=ligegyldig'">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Andet
</td>
</tr>
</table>
</div>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<!-- Nyhedssektionen slut-->
<!-- Madopskrifter start-->
<br>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<table border="0" width="164" bgcolor="#D1DBE3" bordercolor="#D1DBE3">
<tr>
<td class="text" width="100%">
<strong> Madopskrifter</strong>
</td>
</tr>
</table>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Opskrifter
</td>
</tr>
</table>
</div>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Søg
</td>
</tr>
</table>
</div>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Tilføj opskrift
</td>
</tr>
</table>
</div>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<!-- Madopskrifter slut -->
<!-- Drinks start -->
<br>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<table border="0" width="164" bgcolor="#D1DBE3" bordercolor="#D1DBE3">
<tr>
<td class="text" width="100%">
<strong> Drinks Opskrifter</strong>
</td>
</tr>
</table>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Drinks
</td>
</tr>
</table>
</div>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Søg
</td>
</tr>
</table>
</div>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Tilføj en drink
</td>
</tr>
</table>
</div>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<!-- Drinks slut -->
<!-- Madudbringning Hele landet start -->
<br>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<table border="0" width="164" bgcolor="#D1DBE3" bordercolor="#D1DBE3">
<tr>
<td class="text" width="100%">
<strong> Madudbringning</strong>
</td>
</tr>
</table>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Food delivery
</td>
</tr>
</table>
</div>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Søg
</td>
</tr>
</table>
</div>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="164">
<tr>
<td class="text" width="100%">
Tilføj Leverandør
</td>
</tr>
</table>
</div>
<img src="img/skillelinie.gif" width="164" height="1" alt="" border="0"><br>
<!-- Drinks slut -->
<!-- Table Afslutning-->
</td>
[slut kode]
MAIN.PHP
[start kode]
<?
function db_connect ()
{
$host = "localhost";
$user = "xxxxx";
$pass = "xxxxxx";
$dbase = "xxxxxxx";
if (!($link = mysql_connect ($host, $user, $pass)))
{
echo("(1)mysql error: " . mysql_errno() . " " . mysql_error());
return 0 ;
}
if(!(mysql_select_db($dbase, $link)))
{
echo("(2)mysql error: " . mysql_errno() . " " . mysql_error());
return 0 ;
}
return $link;
};
function db_query ($sql, $link)
{
if (!($result = mysql_query($sql, $link)))
{
echo("(1)mysql error: " . mysql_errno() . " " . mysql_error());
return 0 ;
}
return $result;
};
$ip = $_SERVER['REMOTE_ADDR'];
switch ($_GET['mode']) {
case 'login':
db_connect();
if(!isset($_POST['Submit'])) { //Tester om brugeren kom fra login.html
echo "Du har ikke brugt login formen.";
}else{
$postbruger = $_POST['brugernavn'];
$postpass = $_POST['adgangskode']; //For læservenligheden overføres post variablerne til normale variabler
$resultat = mysql_query("SELECT Id FROM `brugere` WHERE brugernavn = '$postbruger' AND adgangskode = '$postpass'");//Spørger efter ID
$number = mysql_num_rows($resultat);//Tæller antaller af resultater
if($number == 1) { //Hvis der kun er et resultat, bliver brugeren logget in
$_SESSION['logget_ind'] = 1;
$_SESSION['brugernavn'] = $postbruger;
$_SESSION['adgangskode'] = $postpass; //Sætter session variablerne
include('inc/main/loginsys/loggedin.php');
}else echo"De indtastede oplysninger er ikke korrekte";
};
break;
case 'allnews':
Include('inc/main/news/allnews.php');
$link = db_connect();
$location = $_SERVER['QUERY_STRING'];
$sql = "UPDATE online_users SET location = '/index.php?$location' WHERE ip ='$ip'";
db_query($sql,$link);
break;
case 'news':
switch ($_GET['kat']) {
case 'it':
include('inc/main/news/it/it.php');
$link = db_connect();
$sql = "UPDATE online_users SET location = '/index.php?mode=news&kat=it' WHERE ip ='$ip'";
db_query($sql,$link);
break;
case 'film':
include('inc/main/news/film/film.php');
$link = db_connect();
$sql = "UPDATE online_users SET location = '/index.php?mode=news&kat=film' WHERE ip ='$ip'";
db_query($sql,$link);
break;
case 'generelt':
include('inc/main/news/generelt/generelt.php');
$link = db_connect();
$sql = "UPDATE online_users SET location = '/index.php?mode=news&kat=generelt' WHERE ip ='$ip'";
db_query($sql,$link);
break;
case 'ligegyldig':
include('inc/main/news/ligegyldig/ligegyldig.php');
$link = db_connect();
$sql = "UPDATE online_users SET location = '/index.php?mode=news&kat=ligegyldig' WHERE ip ='$ip'";
db_query($sql,$link);
break;
};
break;
case 'laesnews':
$IDnews = $_GET['ID'];
$kategori = $_GET['kat'];
$link = db_connect();
$sql = "UPDATE online_users SET location = '/index.php?mode=laesnews&kat=$kategori&ID=$IDnews' WHERE ip ='$ip'";
db_query($sql,$link);
$link = db_connect();
$sql = "SELECT * FROM news WHERE kategori = '$kategori' AND ID = '$IDnews'";
$res = db_query($sql, $link);
while($tab=mysql_fetch_array($res)) {
$id=$tab["ID"];
$overskrift=$tab["headline"];
$teaser=$tab["teaser"];
$nyhed= nl2br($tab["bodytext"]);
$navn=$tab["skribent"];
$kilde=$tab["kilde"];
$dato=$tab["dato"];
echo "
<br>
<center>
<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td width=\"597\"><img src=\"img/boxcorner_top.gif\"></td>
</tr>
</table>
<table cellpadding=\"0\" cellspacing=\"0\" align=\"center\">
<tr>
<td class=\"boxtop2\" width=\"67\" height=\"19\"><img src=\"img/boxcorner_bottom.gif\"></td>
<td class=\"boxtop1\" background=\"img/boxtopbg.gif\" width=\"528\" height=\"19\"><div class=\"text\">$overskrift</div></td>
</tr>
</table>
<table class=\"boxmidt\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">
<tr>
<td valign=\"top\" width=\"595\" height=\"*\">
<table bgcolor=\"#FAFAFA\">
<tr>
<td style=\"padding: 5px;\" class=\"text\"><b>$teaser</b> <br><br><br><br> $nyhed</td></tr>
</table>
</td>
</tr>
<tr>
<td class=\"forfatter\" height=\"19\" width=\"595\" background=\"img/boxbottombg.gif\"><div class=\"text\" style=\"padding-left: 5px;\">Skrevet d. $dato af $navn</div></td>
</tr>
</table>
</center>
";
};
echo "<br><br><br><br><br>";
$sql = "SELECT * FROM comments WHERE NewsID = $id ORDER BY id";
$res = db_query($sql, $link);
$cmtnr = 1;
while($tab=mysql_fetch_array($res)) {
$comment=$tab["CommentBody"];
$bruger=$tab["user"];
$dato=$tab["dato"];
echo "
<center>
<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"500\">
<tr>
<td align=\"left\" class=\"text\" background=\"img/commentbg.gif\" style=\"border-top: 1px solid #84A8BE; border-left: 1px solid #84A8BE; padding-left: 5px;\">Kommentar #$cmtnr:</td>
<td align=\"right\" class=\"text\" background=\"img/commentbg.gif\" style=\"border-right: 1px solid #84A8BE; border-top: 1px solid #84A8BE;\">$dato</td>
</tr>
<tr>
<td colspan=\"2\" class=\"text\" style=\" border-right: 1px solid #84A8BE; border-left: 1px solid #84A8BE; padding: 5px;\">$comment</td>
</tr>
<tr>
<td align=\"left\"colspan=\"2\" class=\"text\" style=\" border-right: 1px solid #84A8BE; border-bottom: 1px solid #84A8BE; border-left: 1px solid #84A8BE; padding: 5px;\">Bruger: $bruger</td>
</tr>
</table>
</center>
<br>
";
$cmtnr = $cmtnr + 1;
};
break;
case 'online':
switch ($_GET['profile']) {
case 'guest':
$link = db_connect();
$sql = "UPDATE online_users SET location = '/index.php?mode=online&profile=guest' WHERE ip ='$ip'";
db_query($sql,$link);
include('inc/main/uo/uo.php');
break;
};
break;
};
?>
[slut kode]
RIGHTMENU.PHP
[start kode]
<?php
$mysql_host = "localhost";
$mysql_user = "xxxxx";
$mysql_password = "xxxx";
$ip = $_SERVER['REMOTE_ADDR'];
$name = gethostbyaddr($_SERVER['REMOTE_ADDR']);
mysql_connect($mysql_host,$mysql_user,$mysql_password);
mysql_select_db("xxxxxx");
$time = explode(" ",microtime());
$array1 = $time[0];
$array2 = $time[1];
$location = $_GET['QUERY_STRING'];
if ($location == null) {
$location = "/index.php";
};
$r = mysql_fetch_row( mysql_query("SELECT * FROM online_users WHERE ip like '$ip'"));
if ($r == (false)) {
mysql_query("INSERT INTO online_users (id,ip,name,location,date) values ('','$ip','$name','$location','$array2')");
}
mysql_query("DELETE FROM online_users where date < $array2 - 400");
$res = mysql_query("SELECT * FROM online_users");
$tal = mysql_num_rows($res);
$dDagNumMaaned = date('j'); // Dagens nummer i indeværende måned
// (et tal mellem 1-31, begge iregnet)
$dMaanedNumAar = date('n'); // Måneds nummer i året
//(et tal mellem 1-12, begge iregnet)
$dAarNu = date('Y'); // Indeværende år (4 cifre)
$dDagNumUge = date('w'); // Dagens nummer i indeværende uge
// (et tal mellem 0-6, begge iregnet)
$dagNavn[] = "Søndag";
$dagNavn[] = "Mandag";
$dagNavn[] = "Tirsdag";
$dagNavn[] = "Onsdag";
$dagNavn[] = "Torsdag";
$dagNavn[] = "Fredag";
$dagNavn[] = "Lørdag";
$maanedNavn[] = "Januar";
$maanedNavn[] = "Februar";
$maanedNavn[] = "Marts";
$maanedNavn[] = "April";
$maanedNavn[] = "Maj";
$maanedNavn[] = "Juni";
$maanedNavn[] = "Juli";
$maanedNavn[] = "August";
$maanedNavn[] = "September";
$maanedNavn[] = "Oktober";
$maanedNavn[] = "November";
$maanedNavn[] = "December";
$dagNavn = htmlentities($dagNavn[$dDagNumUge]);
$maanedNavn = htmlentities($maanedNavn[$dMaanedNumAar-1]);
?>
<?php
$aarnr = date(Y);
$maanednr = date(m);
$dagnr = date(d);
function ugenummer ($y, $m, $d) {
$dato = "$y-$m-$d"; // ÅÅÅÅ-MM-DD
$ugenummer = date("W",strtotime($dato));
return ($ugenummer);
}
?>
<br>
<img src="img/skillelinie2.gif" width="163" height="1" alt="" border="0"><br>
<table border="0" width="163" bgcolor="#D1DBE3" bordercolor="#D1DBE3">
<tr>
<td class="text" width="100%">
<strong> Community</strong>
</td>
</tr>
</table>
<img src="img/skillelinie2.gif" width="163" height="1" alt="" border="0"><br>
<?
if($_SESSION['logget_ind'] == 1) {//Hvis brugeren er logget ind
echo var_dump( isset($_SESSION['logget_ind']) );
include('inc/main/loginsys/community.php');
}else{
echo var_dump( isset($_SESSION['logget_ind']) );
include('inc/main/loginsys/login.php');
};
?>
<img src="img/skillelinie2.gif" width="163" height="1" alt="" border="0"><br>
<table border="0" width="163" bgcolor="#D1DBE3" bordercolor="#D1DBE3">
<tr>
<td class="text" width="100%">
<strong> Statistik</strong>
</td>
</tr>
</table>
<img src="img/skillelinie2.gif" width="163" height="1" alt="" border="0"><br>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';" onclick="location.href='?mode=online&profile=guest'">
<table border="0" width="163">
<tr>
<td class="text" width="100%">
Gæster online:
<?
if ($tal <= "1") {
print "1";
}
else {
print $tal;
};
?>
</td>
</tr>
</table>
</div>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="163">
<tr>
<td class="text" width="100%">
Profiler online: 70<br>
</td>
</tr>
</table>
</div>
<div onmouseover="style.backgroundColor='#D1DBE3'; style.bordercolor='#D1DBE3'; style.cursor='hand';" onmouseout="style.backgroundColor='transparent';">
<table border="0" width="163">
<tr>
<td class="text" width="100%">
Profiler ialt: 70<br>
</td>
</tr>
</table>
</div>
<img src="img/skillelinie2.gif" width="163" height="1" alt="" border="0"><br>
<table border="0" width="163">
<tr>
<td class="text" width="100%">
<?php echo "$dagNavn d. $dDagNumMaaned. $maanedNavn"; ?><br>
<?php echo "$dAarNu";?><br>
Uge: <?php echo ugenummer(£aarnr, £maanednr, $dagnr); ?>
</td>
</tr>
</table>
<img src="img/skillelinie2.gif" width="163" height="1" alt="" border="0"><br>
<table border="0" width="163">
<tr>
<td class="text" width="100%">
Genereringstid: <strong><? printf("%.4f", $CPUtime); ?>s.</strong><br>
</td>
</tr>
</table>
<img src="img/skillelinie2.gif" width="163" height="1" alt="" border="0"><br>
</tr>
</table>
</td>
</tr>
</table>
[slut kode]
Det skulle være det. Jeg kan ikke gennemskue hvad, eller hvor problemet ligger.
Hvis nogen skulle være interreseret kan de se siden her: http://80.167.220.243/fjappedk/
Håber virkelig der er nogen derude, der har mere forstand på det end jeg har.
