I didn't have session_start(); at the start of all my files. I also had some issues with using session_id(); Thank you everyone.
Ok I have a few problems with what I’m writing. I am working on trying to my session data to work across multipule pages. I have been reading up on sessions, but I don’t see anything stating about what the domain of a session is. I have other PHP programs running on my site and I don’t want them or my session variables effecting eachother.
Currently I am having just the opposite problem. I use session_start(); in my very fist php page and then I try to use the veriables $_SESSION['valid'] in my require("functions.php"); file. Well over all, my variables don’t seem to be getting passed.
So far I am using three files for all this, the main page (index.php). The file holding all my functions (functions.php) and a login page (login.php).
(Index.php)
<?php
//starts the session
if(session_id()== "")
{
session_start();
$_SESSION['valid'] = 0;
}
//include the files needed for the menu program
require ("functions.php");
//Check to see if install.php exists. If not then config.php is setup for the database.
$filename = 'install.php';
if (file_exists($filename)) {
include("install.php");
} else {
//config.php holds all the database setup info form when the install process was started
require("config.php");
mysql_connect("$ServerURL", "$DBUser", "$UserPass") or die(mysql_error());
mysql_select_db("$DBName") or die(mysql_error());
//Checks to see if the vars, users, and menu tables are made and if not then makes them, and makes the default admin user.
tableExist();
//Make a table with the menu items in it.
fillMenu();
//Add navagation links at the bottom of the page
nav();
}
?>
This is the part in my functions.php file that isn’t working. I want Login to show when the user isn’t logged in and logout when they are.
(functions.php)
//Add navagation links at the bottom of the page
function nav()
{
echo "<div align=\"center\">";
echo "<table width=\"100%\" border=\"0\">";
if(isset($_SESSION['valid']))
{
if ($_SESSION['valid'] == 1)
{
echo "<tr>";
echo "<td><a href=\"logout.php\">Logout</a></td>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
echo "</table>";
echo "</div>";
} else {
echo "<tr>";
echo "<td><a href=\"login.php\">Login</a></td>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
} else {
include ("config.php");
echo "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=$MenuLoc\"> ";
}
}
Here is my page that sets the $_SESSION['valid'] if they are a valid user.
(login.php)
//database code goes here, but works and isn’t need to be shown.
if ($row['name'] == $UserName && $row['pass'] == $UserPassword && $UserName != "")
{
//Set the cookie so it knows your a valid user.
$_SESSION['valid'] = 1;
// query was legal and could be executed by the server
$SessionUrl = "$MenuLoc/index.php?PHPSESSID=$PHPSESSID";
echo "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL = $SessionUrl";
}else{
if ($UserName != "")
{
// something wrong, so:
echo "</p>";
echo "<p> </p>";
echo "<p><strong><font color=\"#FF0000\" size=\"+2\">Invalid Entry</font></strong></p>";
}
}