Jump to content

Display problems


genista

Recommended Posts

Hi all,

I was testing a script just now and quickly wanted to see if I could display the userid from the following code:

[code=php:0]
print("Your User ID Number is: <b>".$_SESSION["userid"]."</b><br>");
[/code]

However, nothing gets displayed - its blank, I have an include file that sets up the session and the code for that looks like so:

[code=php:0]
session_register("userid");
session_register("username");
session_register("password");
session_register("loggedIn");
[/code]

If I run the same code at the top but change it to username it displays fine.

ANy ideas?

G
Link to comment
Share on other sites

Sounds like it's not set then.. what happens if you set the session above also?
$_SESSION["userid"]='123';
print("Your User ID Number is: <b>".$_SESSION["userid"]."</b><br>");


If that works then i would say check the rest of your code as you say the username is registering fine..


Liam
Link to comment
Share on other sites

Ok, I think I have found the problem in that in my login script it is not setting up the userid as part of the session. However on trying to add the userid to the session like so:

[code=php:0]
checkLoggedIn("no");

// Page title:
$title="Member Login Page";

// if $submit variable set, login info submitted:
if(isset($_POST["submit"])) {
    //
    // Check fields were filled in
    //
    // login must be between 4 and 15 chars containing alphanumeric chars only:
    field_validator("username", $_POST["username"], "alphanumeric", 4, 15);
    // password must be between 4 and 15 chars - any characters can be used:
    field_validator("password", $_POST["password"], "string", 4, 15);

    // if there are $messages, errors were found in validating form data
    // show the index page (where the messages will be displayed):
    if($messages){
        doIndex();
        // note we have to explicity 'exit' from the script, otherwise
        // the lines below will be processed:
        exit;
    }

    // OK if we got this far the form field data was of the right format;
    // now check the user/pass pair match those stored in the db:
    /*
    If checkPass() is successful (ie the username and password are ok),
    then $row contains an array of data containing the login name and
    password of the user.
    If checkPass() is unsuccessful however, $row will simply contain
    the value 'false' - and so in that case an error message is
    stored in the $messages array which will be displayed to the user.
    */
    if( !($row = checkPass($_POST["username"], $_POST["password"])) ) {
        // username/passwd string not correct, create an error message:
        $messages[]="Incorrect username/password, try again";
    }

    /*
    If there are error $messages, errors were found in validating form data above.
    Call the 'doIndex()' function (which displays the login form) and exit.
    */
    if($messages){
        doIndex();
        exit;
    }

    /*
    If we got to this point, there were no errors - start a session using the info
    returned from the db:
    */
    cleanMemberSession($row["username"], $row["password"], $row["userid"]);

    // and finally forward user to members page (populating the session id in the URL):
    header("Location: members.php?".session_name()."=".session_id());
} else {   
    // The login form wasn't filled out yet, display the login form for the user to fill in:
    doIndex();
}

/*
This function displays the default 'index' page for this script.  This consists of just a simple
login form for the user to submit their username and password.
*/
function doIndex() {
    /*
    Import the global $messages array.
    If any errors were detected above, they will be stored in the $messages array:
    */
    global $messages;

    /*
    also import the $title for the page - note you can normally just declare all globals on one line
    - ie:
    global $messages, $title;
    */
    global $title;

    // drop out of PHP mode to display the plain HTML:
?>[/code]

I get an undefined index error on the userid, so how do I create the index on userid, on the basis that the user won't be entering this information along with their username and password when they login?
Link to comment
Share on other sites

I use: [code=php:0]checkLoggedIn("yes");[/code] this relates to a function which all works fine. I dont understand by your comment:

"You shoud use, $_SESSION['var_name'] = 'some value';, instead"

How could this be used to index the userid?
Link to comment
Share on other sites

session_register('userid') does nothing but creates an emtpty (null) session called userid. If you do $_SESSION['userid'] = 'userid';
The userid session will be set to 'userid';

Is this question related to your question I was helping you the othere day with about getting the userid?
Link to comment
Share on other sites

Hi Wildteen, thanks for that I will try it. This kinda relates to the question the other day, only I need to get this part right before I can solve the question I had the other day, hence the new post (and hopefully I can solve my previous problem once this is sorted).
Link to comment
Share on other sites

Ok w'ere on the right track, except by using this:

[code=php:0]
cleanMemberSession($row["username"], $row["password"], $_SESSION['userid'] = 'userid');

    // and finally forward user to members page (populating the session id in the URL):
    header("Location: members.php?".session_name()."=".session_id());
} else {   
    // The login form wasn't filled out yet, display the login form for the user to fill in:
    doIndex();
}
[/code]

On any subsequent page where I ask it to display the userid it will simply display 'userid,' rather than any database record.
Link to comment
Share on other sites

Ok, here it is:

[code=php:0]
cleanMemberSession($row["username"], $row["password"], $_SESSION["userid"]);

    // and finally forward user to members page (populating the session id in the URL):
    header("Location: members.php?".session_name()."=".session_id());
} else {   
    // The login form wasn't filled out yet, display the login form for the user to fill in:
    doIndex();
}
[/code]

Thanks,

G
Link to comment
Share on other sites

Ok here is checkpass:

[code=php:0]
function checkPass($username, $password) {
    /*
    Password checking function:
    This is a simple function that takes the $username and
    $password that a user submits in a form and checks that a
    row exists in the database where:

    the value of the 'login' column is the same as the value in $login
    and
    the value of the 'password' column is the same as the value in $password

    If exactly one row is returned, then that row of data is returned.
    If no row is found, the function returns 'false'.
    */
    global $link;
   
    $query="SELECT username, password FROM users WHERE username='$username' and password='$password'";
    $result=mysql_query($query, $link)
        or die("checkPass fatal error: ".mysql_error());
   
    // Check exactly one row is found:
    if(mysql_num_rows($result)==1) {
        $row=mysql_fetch_array($result);
        return $row;
    }
    //Bad Login:
    return false;
} // end func checkPass($username, $password)
[/code]


And here is cleanmembersession:

[code=php:0]
function cleanMemberSession($username, $password) {
    /*
    Member session initialization function:
    This function initializes 3 session variables:
  $login, $password and $loggedIn.

    $login and $password are used on member pages (where you
    could allow the user to change their password for example).

    $loggedIn is a simple boolean variable which indicates
    whether or not the user is currently logged in.
    */
    $_SESSION["username"]=$username;
    $_SESSION["password"]=$password;
    $_SESSION["loggedIn"]=true;
} // end func cleanMemberSession($username, $pass)
[/code]


Wait a Minute! If I am not mistaken what I should do is add userid to the cleanmembersession to get it working right?
Link to comment
Share on other sites

OKay change this:
[code]$query="SELECT username, password FROM users WHERE username='$username' and password='$password'"[/code]
to:
[code]$query="SELECT userid, username, password FROM users WHERE username='$username' and password='$password'"[/code]

and change this:
[code]function cleanMemberSession($username, $password) {[/code]
to:
[code]function cleanMemberSession($username, $password, $userid) {[/code]
Now add [code=php:0]$_SESSION['userid'] = $userid;[/code] after [code=php:0]$_SESSION["loggedIn"]=true;[/code]

Your code should now get the userid.
Link to comment
Share on other sites

Ok I have made those changes, but in my members page where I want to display the userid, nothing is coming up still....

[code=php:0]
// Check user logged in already:
checkLoggedIn("yes");
doCSS();



print("Welcome to the members page <b>".$_SESSION["username"]."</b><br>");
print("Your password is: <b>".$_SESSION["password"]."</b><br>");
print("Your User ID Number is: <b>".$_SESSION["userid"]."</b><br>");
print("<a href=\"logout.php?".session_name()."=".session_id()."\">Logout</a></b><br>");
print ("<a href=\"suppliersearch.php?".session_name()."=".session_id()."\">Search for suppliers</a></b></br>");
[/code]

You will find below the two functions that needed to be updated:

[code=php:0]
function checkPass($username, $password) {
    /*
    Password checking function:
    This is a simple function that takes the $username and
    $password that a user submits in a form and checks that a
    row exists in the database where:

    the value of the 'login' column is the same as the value in $login
    and
    the value of the 'password' column is the same as the value in $password

    If exactly one row is returned, then that row of data is returned.
    If no row is found, the function returns 'false'.
    */
    global $link;
   
    $query="SELECT userid, username, password FROM users WHERE username='$username' and password='$password'";
    //$query="SELECT username, password FROM users WHERE username='$username' and password='$password'";
    $result=mysql_query($query, $link)
        or die("checkPass fatal error: ".mysql_error());
   
    // Check exactly one row is found:
    if(mysql_num_rows($result)==1) {
        $row=mysql_fetch_array($result);
        return $row;
    }
    //Bad Login:
    return false;
} // end func checkPass($username, $password)
[/code]

and:

[code=php:0]
function cleanMemberSession($username, $password, $userid) {
    /*
    Member session initialization function:
    This function initializes 3 session variables:
  $login, $password and $loggedIn.

    $login and $password are used on member pages (where you
    could allow the user to change their password for example).

    $loggedIn is a simple boolean variable which indicates
    whether or not the user is currently logged in.
    */
    $_SESSION["username"]=$username;
    $_SESSION["password"]=$password;
    $_SESSION["loggedIn"]=true;
$_SESSION["userid"]=$userid;
} // end func cleanMemberSession($username, $pass)
[/code]
Link to comment
Share on other sites

When you call the cleanMerberSessions function do you call it like this:
[code=php:0]cleanMemberSession($row["username"], $row["password"], $row["userid"]);[/code]

Also is the fields that stores the user id number called [b]userid[/b] within the user table?
Link to comment
Share on other sites

Hi all (again)

After adding the userid to the flushmember function I now have a new problem that is causing some heair pulling..

The function does not work when a user 'joins', by using a join form the user gets directed to the memebrs page which is what you have been helping on. However when I now try and register a user I get the following errors:

Warning: Missing argument 3 for cleanmembersession() in site.co.uk/user/htdocs/functions.php on line 231

Notice: Undefined variable: userid in site.co.uk/user/htdocs/functions.php on line 245


This part of the code looks like so:

[code=php:0]
function cleanMemberSession($username, $password, $userid) {
    /*
    Member session initialization function:
    This function initializes 3 session variables:
  $login, $password and $loggedIn.

    $login and $password are used on member pages (where you
    could allow the user to change their password for example).

    $loggedIn is a simple boolean variable which indicates
    whether or not the user is currently logged in.
    */
    $_SESSION["username"]=$username;
    $_SESSION["password"]=$password;
    $_SESSION["userid"]=$userid;
$_SESSION["loggedIn"]=true;
[/code]

If I take out the userid the members page stops displaying the userid, if I add it I get these errors. I have no idea on a workaround, unless I can create another function that pulls in the userid?
Link to comment
Share on other sites

Ok here is it s off the join form:

[code=php:0]
cleanMemberSession($_POST["username"], $_POST["password"], $_POST["userid"]);

        // and then redirect them to the members page:
        header("Location: members.php?".session_name()."=".session_id());

     
    }
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.