Jump to content


Photo

Display problems


  • Please log in to reply
23 replies to this topic

#1 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 09 August 2006 - 09:20 AM

Hi all,

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

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

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

session_register("userid");
session_register("username");
session_register("password");
session_register("loggedIn");

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

ANy ideas?

G

#2 dual_alliance

dual_alliance
  • Members
  • PipPipPip
  • Advanced Member
  • 140 posts
  • LocationNSW, Australia

Posted 09 August 2006 - 09:29 AM

Put this at the top of the page.

<?php
print_f($_SESSION);
?>

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

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 09 August 2006 - 09:30 AM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 09 August 2006 - 10:03 AM

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:

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:
?>

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?

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 10:11 AM

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.

#6 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 09 August 2006 - 10:28 AM

I use:
checkLoggedIn("yes");
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?

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 10:40 AM

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?

#8 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 09 August 2006 - 10:46 AM

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).

#9 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 09 August 2006 - 11:01 AM

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

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();
}

On any subsequent page where I ask it to display the userid it will simply display 'userid,' rather than any database record.

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 12:33 PM

Post the function here that queries the database for when the user logs in.

#11 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 09 August 2006 - 12:58 PM

Ok, here it is:

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();
}

Thanks,

G

#12 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 02:39 PM

Post the code for the following function:
checkPass
cleanMemberSession

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

#13 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 09 August 2006 - 02:52 PM

Ok here is checkpass:

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) 


And here is cleanmembersession:

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)


Wait a Minute! If I am not mistaken what I should do is add userid to the cleanmembersession to get it working right?

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 03:01 PM

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

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

Your code should now get the userid.

#15 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 10 August 2006 - 09:47 AM

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

// 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>");

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

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) 

and:

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)


#16 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 10 August 2006 - 10:57 AM

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

Also is the fields that stores the user id number called userid within the user table?

#17 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 10 August 2006 - 11:09 AM

Yee ha! Wildteen you are great! Thank you for your help, userid is now displayed (it was the cleanmembersession that was wrong...).

#18 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 10 August 2006 - 11:23 AM

No problem. Glad you got there in the end.

#19 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 10 August 2006 - 02:12 PM

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:

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;

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?

#20 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 10 August 2006 - 02:16 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users