Migrated from MySQL 4 to 5 (now session access doesn't work)

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
matrixx1
Forum Newbie
Posts: 8
Joined: Fri Apr 15, 2011 1:04 am

Migrated from MySQL 4 to 5 (now session access doesn't work)

Post by matrixx1 »

I have a site with a members area that uses sessions. I migrated my DB from MySQL 4 to 5. Now my login page does not work. Please help.

Member Login Page:

Code: Select all

<?php
session_name ('name');
ini_set ('session.use_cookies', 0);
session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/swfobject/swfobject.js"></script>
<script type="text/javascript">
		var flashvars = {};
		flashvars.xml = "config.xml";
		flashvars.font = "font.swf";
		var attributes = {};
		attributes.wmode = "transparent";
		attributes.id = "slider";
		swfobject.embedSWF("cu3er.swf", "cu3er-container", "960", "400", "9", "expressInstall.swf", flashvars, attributes);
</script>
<link href="css/menu.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#apDiv1 {
	position:absolute;
	left:592px;
	top:75px;
	width:552px;
	height:53px;
	z-index:1;
}
a:visited {
	color: #5D4580;
}
a:hover {
	color: #FFF;
}
a:active {
	color: #5D4580;
}
</style>
</head>
<body>
<div class="main">
  <div class="blok_header">
    <div class="header">
      <div class="rss"><strong>P</strong></div>
      <div class="clr"></div>
      <div class="logo"><a href="index.html"><img src="images/logo.jpg" width="211" height="88" border="0" alt="" class="one" /></a></div>
      <div class="menu">
        <ul id="css3menu">
          <li class="topfirst"><a href="index.html" title="Home">Home</a></li>
          <li><a href="#" title=""><span>Omega Psi Phi</span></a>
            <ul>
              <li><a href="founders.html" title="Fraternity Founders">Fraternity Founders</a></li>
              <li><a href="omegahistory.html" title="Fraternity History">Fraternity History</a></li>
              <li><a href="programs.html" title="Mandated Programs">Mandated Programs</a></li>
              <li><a href="links.html" title="Links">Links</a></li>
            </ul>
          </li>
          <li><a href="#" title="Phi Gamma Chapter"><span>Phi Gamma Chapter</span></a>
            <ul>
              <li><a href="history.html" title="Phi Gamma History">Phi Gamma History</a></li>
              <li><a href="lineage.html" title="Phi Gamma Lineage">Phi Gamma Lineage</a></li>
              <li><a href="dedication.html" title="Dedication">Dedication</a></li>
              <li><a href="calendar.html" title="Calendar">Calendar</a></li>
              <li><a href="photogallery.html" title="Photo Gallery">Photo Gallery</a></li>
              <li><a href="members/login.php" title="Member Login">Member Login</a></li>
              <li><a href="roster.html" title="Chapter Roster">Chapter Roster</a></li>
            </ul>
          </li>
          <li><a href="members/login.php" title="Member Login">Member Login</a></li>
          <li class="toplast"><a href="contact.html" title="Contact Info.">Contact Info.</a></li>
        </ul>
      </div>
     
  </div>
  <div class="clr"></div>
  <div class="body">
    <div class="body_bg">
      <h2>Member's Only      </h2>
     <?php 
if ($_SESSION['membername'] != null) {
	//	print "<META HTTP-EQUIV='Refresh' content='0;URL=updatemember.php'>";
	echo "You are already logged in. Please wait to be redirected to the members page, or <a href=\"members.php\">click here</a> if you are not automatically redirected. User name for the session is ".$_SESSION['username'].", the member name is ".$_SESSION['membername'];
	print "<META HTTP-EQUIV='Refresh' content='5;URL=members.php'>";
	//header("Location: members.php");
	//exit;

} else {
?>
<form name="admin" method="post" action="userlogon.php">
  <table width="891" border="0" cellspacing="5" cellpadding="5">
    <tr>
      <td height="80" colspan="2" align="left"><p>Please enter you username and password.  Don't remember your password,  <u><strong><a href="password.php">click here</a></strong></u><strong><a href="password.php"></a></strong>.</td>
      <td width="360" rowspan="5" align="center" valign="middle"><img src="images/login_image.jpg" width="239" height="314" /></td>
    </tr>
    <tr>
      <td width="257" height="38" align="right"><p><strong>Username:</strong></td>
      <td width="224" align="left"><input type="text" name="username"></td>
      </tr>
    <tr>
      <td height="38" align="right"><p><strong>Password:</strong></td>
      <td align="left"><input type="password" name="password"></td>
      </tr>
    <tr>
      <td height="34">&nbsp;</td>
      <td  align="left"><input type="submit" class="yellowbutton" name="submit" value="Sign In"></td>
      </tr>
    <tr>
      <td colspan="2"><p>To register for a username and password with Phi Gamma, please <u><strong><a href="register.php">click here</a></strong></u><strong><a href="password.php"></a></strong>.</td>
      </tr>
  </table>
</form>

<?php
}
?>
      <p>&nbsp;</p>
      <div class="clr"></div>
    </div>
    <div class="clr"></div>
  </div>
  </div>
<div class="footer">
  <div class="footer_resize">
    <p class="center"> <a href="index.html">Home</a> | <a href="contact.html">Contact</a>    </p>
    <div class="clr"></div>
  </div>
  <div class="clr"></div>
</div>
</body>
</html>
After the credentials are entered the user is returned back to the login page and NOT the member's page.

This is the userlogon.php page:

Code: Select all

<?php
$link = mysql_connect('localhost:/tmp/mysql5.sock', 'dbuser', 'dbpw');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}

//mysql_close($link);

if (!mysql_select_db('db_name', $link)) {
   echo 'Could not select database';
   exit;
}


$username =  $HTTP_POST_VARS["username"];
$password =  $HTTP_POST_VARS["password"];
$member_id = '';
$role = '';
$firstname = '';

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()

$query = sprintf("SELECT users.member_id, users.username, users.password, users.role, FROM users join members on users.member_id = members.member_id WHERE users.username ='%s' AND users.password='%s'",
   mysql_real_escape_string($username),
   mysql_real_escape_string($password));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $query;
   die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {

   $member_id = $row['member_id'];
   $username = $row['username'];
 
    $role = $row['role'];

}

if ($member_id != ''){
	session_start();

	$_SESSION['membername'] = $username;
	$_SESSION['username'] = $username;
	
	$_SESSION['memberid'] = $member_id;
	$_SESSION['role'] = $role;

	print "<META HTTP-EQUIV='Refresh' content='0;URL=members.php'>";
} else {
	print "<META HTTP-EQUIV='Refresh' content='0;URL=memberlogin.php'>";
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
matrixx1
Forum Newbie
Posts: 8
Joined: Fri Apr 15, 2011 1:04 am

Re: Migrated from MySQL 4 to 5 (now session access doesn't w

Post by matrixx1 »

CORRECTION!! This is the userlogon page:

Code: Select all

<?php
$link = mysql_connect('localhost:/tmp/mysql5.sock', 'db_user', 'db_pw');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}

//mysql_close($link);

if (!mysql_select_db('db_name', $link)) {
   echo 'Could not select database';
   exit;
}


$username =  $HTTP_POST_VARS["username"];
$password =  $HTTP_POST_VARS["password"];
$member_id = '';
$role = '';
$firstname = '';

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()

$query = sprintf("SELECT users.member_id, users.username, users.password, users.role, members.firstname FROM users join members on users.member_id = members.member_id WHERE users.username ='%s' AND users.password='%s'",
   mysql_real_escape_string($username),
   mysql_real_escape_string($password));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $query;
   die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {

   $member_id = $row['member_id'];
   $username = $row['username'];
   $firstname = $row['firstname'];
    $role = $row['role'];

}

if ($member_id != ''){
	session_start();

	$_SESSION['membername'] = $username;
	$_SESSION['username'] = $username;
	$_SESSION['firstname'] = $firstname;
	$_SESSION['memberid'] = $member_id;
	$_SESSION['role'] = $role;

	print "<META HTTP-EQUIV='Refresh' content='0;URL=members.php'>";
} else {
	print "<META HTTP-EQUIV='Refresh' content='0;URL=memberlogon.php'>";
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Migrated from MySQL 4 to 5 (now session access doesn't w

Post by Jade »

That stands to reason that the member's login information is missing from the database not that it's a session problem. It could also be that you're not starting your session on the members.php and that is what's redirecting you back to the login page.
matrixx1
Forum Newbie
Posts: 8
Joined: Fri Apr 15, 2011 1:04 am

Re: Migrated from MySQL 4 to 5 (now session access doesn't w

Post by matrixx1 »

Thanks for the reply. Password is definitely in the db. The visitor is actually not getting to members.php at all. They are being redirected from memberlogin.php

I changed this to another page to test it and sure enough when I attempted to login, I was redirected to the test page.

Code: Select all

} else {
	print "<META HTTP-EQUIV='Refresh' content='0;URL=memberlogon.php'>";
}
matrixx1
Forum Newbie
Posts: 8
Joined: Fri Apr 15, 2011 1:04 am

Re: Migrated from MySQL 4 to 5 (now session access doesn't w

Post by matrixx1 »

All info is definitely in both tables of the database (users: member_id, username, password, role / members: firstname, member_id). Do you think the code is not compatible with MySQL 5?
matrixx1
Forum Newbie
Posts: 8
Joined: Fri Apr 15, 2011 1:04 am

Re: Migrated from MySQL 4 to 5 (now session access doesn't w

Post by matrixx1 »

PROBLEM SOLVED.....was entering the wrong password!! sorry for wasting your time and I appreciate the help.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Migrated from MySQL 4 to 5 (now session access doesn't w

Post by Jade »

No problem, it happens to the best of us :D
Post Reply