Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Session Problem

Options
  • 29-12-2006 4:03pm
    #1
    Registered Users Posts: 1,987 ✭✭✭


    Below is the code im using, basicly im passing the session id into the function as '$sess' and then a number as '$num', it checks all entries of the table with a loop and if the entry already exists then it will return true as the '$valid' var, but it always returns false even if the entry exists in the table??

    Any ideas?
    function check_session($sess,$num)
    {
    	$valid = false;
    	$num_rows = count_ip();
    	for ($counter = 1; $counter <= $num_rows; $counter++)
    	{
    		$result = mysql_query("SELECT sess_id,num FROM sess_id WHERE id=".$counter."");  
    		$row = mysql_fetch_array( $result );
    		
    		$sessIn = $row['sess_id'];
    		$numIn = $row['num'];
    		
    		if(($sessIn == $sess) && ($numIn == $num))
    		{
    			//Entry already exists
    			$valid = true;
    		}
    	}
    	return $valid;
    }
    


Comments

  • Registered Users Posts: 683 ✭✭✭Gosh


    What's the following doing?

    [PHP]
    $num_rows = count_ip();
    [/PHP]

    this controls the loop iterations and if it's zero or less than zero then the loop won't even start


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    That just calls a method to count the lines in the table to tell the loop how many times to run through:
    function count_ip()
    {
    	$sql = 'SELECT COUNT(id) AS numrows FROM sess_id';
    	$data = mysql_query($sql);
    	$row     = mysql_fetch_array($data, MYSQL_ASSOC);
    	$numrows = $row['numrows'];
    	
    	return $numrows;
    }
    


  • Registered Users Posts: 683 ✭✭✭Gosh


    There isn't a need to read every row - you could use the following

    [PHP]
    function check_session($sess,$num)
    {
    $valid = false;
    $result = mysql_query("SELECT sess_id,num FROM sess_id WHERE sess_id='".$sess."' AND num='".$num."' LIMIT 1");
    while ($row = mysql_fetch_object( $result )) {
    //Entry already exists
    $valid = true;
    }
    return $valid;
    }
    [/PHP]


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    What does the 'LIMIT 1' do?


  • Registered Users Posts: 683 ✭✭✭Gosh


    Returns 1 record where the sess_id and num are equal in the select. It's there just to make sure you only return 1 record even if there are duplicates. You are only interested if there is a matching record - that's what the WHILE is doing - returning the row data if it's there and setting the value of $valid to true


  • Advertisement
  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    Nice one man, that works fine now! Thanks!


Advertisement