Hi:
I am trying to create a function that can store the user's info.
Currently I have this:
sessionInfo.inc
===========
<?php
class sessionInfo {
/*
* @(#) sessionInfoBean.java
*
* A class to handle sessions.
*
* @version 10 1 Oct 2003
*
*
*/
/**
* Variables
*/
var $last_name;
var $first_name;
var $userid;
var $email_address;
var $group = array();
function sessionInfo(){
}
/**
* Function to determine the group the user belongs to.
*
* @param groupId Checks if the user is a member of the group.
*
*/
function ismemberof($groupid){
for($i=0;$i<=count($group);$i++) {
if($group[i].equals($groupid)){
return true;
}
}
}
/**
* Method to get the user id to establish a session.
*/
function getuserid(){
return $this ->userid;
}
/**
* Method that returns the available groups.
*/
function getgroups(){
return $this -> group;
}
/**
* Method to set the user id.
* @param userid the user id.
*/
function setuserid($userid){
$this->userid = $userid;
}
/**
* Method to add a new group.
* @param group name of the group.
*/
function addgroup($groupid,$var){
$this ->group[$var] = $groupid;
}
};
?>
Now, in my login.php, I am trying to add the groups that the user
belongs to, like this:
login.php
=======
$username = $_POST['username'];
$password = $_POST['password'];
//include the database connection file
include 'include/dbConnection.php';
$query1 = "select * from users where username='$username' AND
password='$password'";
$result=mysql_query($query1, $conn);
//check that at least one row was returned
$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
while($row = mysql_fetch_array($result)){
//start the session and register a variable
$userid = $row['userid'];
/**
* Set the user's information.
*/
require 'include/sessionInfo.inc';
$sessionVar= new sessionInfo();
$sessionVar->setuserid($userid);
}
mysql_free_result($result);
$query2 = "SELECT group_id FROM user_groups WHERE
userid=".$userid;
$result = mysql_query($query2, $conn);
//Get number of rows returned
$numOfRows = mysql_num_rows($result);
$i=0;
while($row = mysql_fetch_array($result)){
$sessionVar->addgroup($row["group_id"],i);
while($i<numOfRows){
$i .= 1;
}
}
session_start();
$_SESSION['obj'] = $sessionVar;
------------------------------
Now I redirect to a test page, where I am trying to print the no of
groups the user belongs to. (As an example, every user belongs to
atleast 3 groups).
in Test.php
=========
<?php
require 'include/sessionInfo.inc';
session_start();
//get the user_id from sessionInfo.
$sessionVar = $_SESSION['obj'];
echo "User id is :".$sessionVar->getuserid()."<br>";
echo "User name is :".$sessionVar->getfirstname()."
".$sessionVar->getlastname()."<br>";
$var = count($sessionVar->getgroups());
echo "No of Groups User belongs to:".$var."<br>";
?>
Its gives this:
User id is :7
User name is :Test Test
No of Groups User belongs to:0
Any ideas, where I am going wrong ?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php