Jump to content

Archived

This topic is now archived and is closed to further replies.

genista

Display problems

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

Share this post


Link to post
Share on other sites
Put this at the top of the page.

[code=php:0]<?php
print_f($_SESSION);
?>[/code]

It will show you if any of the sessions are working.

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Make sure you have sessions_start(); at the to each page that uses sessions, otherwise your sessions wont load. Also session_register is depreciated. You shoud use, $_SESSION['var_name'] = 'some value';, instead.

Share this post


Link to post
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?

Share this post


Link to post
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?

Share this post


Link to post
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).

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Post the function here that queries the database for when the user logs in.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Post the code for the following function:
checkPass
cleanMemberSession

I dont want the code that calls those functions, but code for those functions.

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Yee ha! Wildteen you are great! Thank you for your help, userid is now displayed (it was the cleanmembersession that was wrong...).

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
No, its not that code thats the problem but the code you use to call the cleanMemberSession function. Post the code here that you use when the user registers.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
I dont think you want to use $_POST['userid'] as you havn't got the users userid from the database. I think you might want to rethink the logic of your code. As to me it appears to be all over the place.

Share this post


Link to post
Share on other sites
Ah yes, I just added that to see if it works, I had taken it off 5 mons ago. Can I make the function conditional, in other words only provide the userid in the session once a user is logged in?

Share this post


Link to post
Share on other sites

×

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.