+ Reply to Thread
Results 1 to 5 of 5

Thread: problem with php session

  1. #1
    miocene2 is offline x10Hosting Member miocene2 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    15

    problem with php session

    I' trying to make a logon script for my site using $_SESSION to store a variable that identifies the user who is logged on.

    I'm trying to use this code to generate a logon form if the session variable is empty:

    PHP Code:
                <?php if(!$_SESSION['grid']){
                echo 
    "<p>Please Log in to access your account</p>";
                echo 
    "<form method=\"post\" action=\"loggedin.php\">";
                echo 
    "<table>";
                echo 
    "<tr><td>Group Name:</td><td><input name=\"group\" type=\"text\" /></td></tr>";
                echo 
    "<tr><td>Group Password:</td><td><input name=\"password\" type=\"password\" />";
                echo 
    "<tr colspan=2><td><input name=\"Submit1\" type=\"submit\" value=\"submit\" /></td></tr>";
                echo 
    "</table></form>";}
                
    ?>
    But my problem is the form is always generated whether or not $_SESSION['grid'] exists.


    I'm also having trouble with my sql queries that used to work fine until I introduced the session stuff:

    For this:
    PHP Code:
    <?php

    include("connection.php");
    $groupid $_SESSION['grid'];
    $username mysql_query("SELECT * FROM users WHERE group = $groupid");
      
    $num_users mysql_num_rows($username);
      
      
    $i=0;
    while(
    $row mysql_fetch_array($username))
      {
      
    $users[$i] = $row['username'];
      
    $user_id[$i] = $row['id'];
      
    $i++;

      }

    ?>
    I get: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/miocene1/public_html/includes/selectusers.phpon line 8

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/miocene1/public_html/includes/selectusers.php on line 12

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: problem with php session

    You need to check if something is set before trying to operate on it. Right now, there's a very good chance that $_SESSION['grid'] is null.

    Are you remembering to start your session with session_start() ?
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: problem with php session

    Additionally, if a query fails, the result is FALSE. This is why you're getting the "supplied argument is not a valid MySQL result resource" error. Check the result before using it to prevent the error. Whatever messages you print for the user, make sure you don't disclose too much information

    mysql is out of date. mysqli is its replacement. Even better is PDO.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    glanceworld is offline x10Hosting Member glanceworld is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    13

    Re: problem with php session

    Please make sure you have open a connection with mysql server. If you already open a connection then run die function like

    PHP Code:
    <?php

    include("connection.php");
    $groupid $_SESSION['grid'];
    $username mysql_query("SELECT * FROM users WHERE group = $groupid") or die (mysql_error());
      
    $num_users mysql_num_rows($username);
      
      
    $i=0;
    while(
    $row mysql_fetch_array($username))
      {
      
    $users[$i] = $row['username'];
      
    $user_id[$i] = $row['id'];
      
    $i++;

      }

    ?>

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: problem with php session

    Quote Originally Posted by glanceworld View Post
    If you already open a connection then run die function like
    Whatever you do, please don't use die() and mysql_error(). die() stops output before the page is finished, producing ill-formed HTML. mysql_error() gives too much information to the user, which I mentioned before is something to avoid. The link is to a more thorough explanation of why not to use die().
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. PHP Problem in Lotus?
    By ezdookie in forum Free Hosting
    Replies: 0
    Last Post: 01-23-2009, 08:49 PM
  2. PHP Upgrade Problem
    By cclworld in forum Free Hosting
    Replies: 4
    Last Post: 12-27-2008, 10:34 AM
  3. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  4. Problem with PHP support ticket system (Fantastico)
    By Jarryd in forum Free Hosting
    Replies: 4
    Last Post: 03-10-2008, 01:35 AM
  5. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers