Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP Session Problem

  • 29-12-2006 04:03PM
    #1
    Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭


    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,991 ✭✭✭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,991 ✭✭✭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,991 ✭✭✭Ziycon


    Nice one man, that works fine now! Thanks!


Advertisement