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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP Session Problem

  • 29-12-2006 3:03pm
    #1
    Registered Users, Registered Users 2 Posts: 1,990 ✭✭✭


    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, Registered Users 2 Posts: 684 ✭✭✭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, Registered Users 2 Posts: 1,990 ✭✭✭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, Registered Users 2 Posts: 684 ✭✭✭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, Registered Users 2 Posts: 1,990 ✭✭✭Ziycon


    What does the 'LIMIT 1' do?


  • Registered Users, Registered Users 2 Posts: 684 ✭✭✭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, Registered Users 2 Posts: 1,990 ✭✭✭Ziycon


    Nice one man, that works fine now! Thanks!


Advertisement