Session Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LoathorHealer
Forum Newbie
Posts: 1
Joined: Sat Mar 18, 2006 11:27 pm

Session Question

Post by LoathorHealer »

Damn Sessions I tell you, awsome tool but F-ing confusing.

So. you know how this forum works? when you're not registered you see " register Search ect." as the links? and then when you register and login, you see Profile and Messages the logout screen?

Thats what I want to do with the sessions. but when I add my sesssions to my page it redirects the user. I tried taking the redirection out..but of course it just gives me a blank page.

heres the code.

Code: Select all

<?php

// For register_global on PHP settings
$member =  $_COOKIE['member'];

session_start();		// you must put this to read session variables
if (empty($member) || !isset($member))	// fail to read the browser cookie
{
	// Try to read session
	if (empty($_SESSION['member']) || !isset($_SESSION['member']))
	{
		header("Location: login.html");	// redirect user to login
		exit;
	}
	else
	{
		$member = $_SESSION['member'];
	}
}
require 'db_connect.php';
$connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");

	// User has login but you should check his account validity again
	$qChk = "select id from membership where name='$member' and status='Y' ";
	$rsChk = mysql_query($qChk);
	
	if (mysql_num_rows($rsChk) != '1')
	{
		session_destroy();
	// make the cookie expires instantly.
		setcookie ("member",$member,time()-1957240,"/"); 
		header("Location: login.html");
		exit;
	}

?>




<html>
<head>
<title> Torque - Your Sports Cars Headquarters</title>
</head>
<link rel="stylesheet" href="tor.css">
<table cellpadding="0" cellspacing="0" width="1000" align="center">
<?php
   print("<tr><td align=\"center\">");
   print("<img src=\"images\banner.jpg\" align=\"center\" style=\"border:1px solid #868686\">");
   print ("</td></tr>");
   print("<tr><td class=\"thead\" width=\"1000\" height=\"29\" style=\"border-bottom:1px solid #868686\" style=\"border-right:1px solid #868686\" style=\"border-left: 1px solid #868686\">");
   print("<table align=\"right\" width=\"1000\" height=\"29\"><tr>");
   print("<td>");
   if(!isset($_GET['page'])) {
$page = 'news';
} else {
$page = $_GET['page'];
}
   print("<a href=\"welcome.php?page=profile\"><img src=\"images\usercp.jpg\" border=\"0\"></a>&nbsp;&nbsp;&nbsp;<img src=\"images\memberlist.jpg\">");
   print("</td><td class=\"contenthead\" width=\"300\">");

  $result = mysql_query("SELECT id, name, car FROM membership");

  while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
printf("<div align=\"right\"><b>Welcome back: %s</b> %s - <a href=\"logout.php\">(Logout)</a></div>", $row["name"], $row["car"]);
}


   print ("</td></tr></table>");
   print ("</td></tr>");

 print("<tr><td>");

echo ("this is a member!");



 include  $page.'.php';
?>
</table>
Im a really big newb so forgive me. but I need help with this.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

session_start();

$member =  (empty($_SESSION['member']) ? $_COOKIE['member'] : $_SESSION['member']);

if (empty($member)) {
   header("Location: login.html");
   exit();
}
Try this
Post Reply