munk.me.uk forum
May 21, 2012, 05:07:10 am *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: SMF - Just Installed!
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Current User's Login  (Read 2499 times)
wintallo
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


View Profile WWW Email
« on: December 03, 2006, 10:05:00 pm »

Hi,

I was wondering how I could display the current user's username on a page other than the members.php page.

Code:
<?php

include_once("config.php");
checkLoggedIn("yes");

$currentuser = $_SESSION["login"];

$lvpassword = "1";

$result = mysql_query("SELECT * FROM users WHERE login ='$currentuser'");
$row = mysql_fetch_array( $result );

if ($_POST['txtPassword'] != $lvpassword) {

?>

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtpassword">Password:</label>
    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

    <p><input type="submit" name="Submit" value="Login" /></p>

</form>
<br />
Your current score is <? echo $row['score']; ?>

<?php

}
else {

?>

<p>Level 1</p>

<?php

}

?>
As you can see, I want to use this user's login name to query a database so I can tell him his score. Thanks for reading! :lol:

Joel  
Logged

~~ Joel Verhag[c
munk
Administrator
Sr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 368


View Profile WWW
« Reply #1 on: December 04, 2006, 01:03:47 am »

Quote
I was wondering how I could display the current user's username on a page other than the members.php page.

I don't quite follow you - you already have the user's username stored in $currentuser or $_SESSION["login"] in the script above so you can just use $currentuser or $_SESSION["login"] to display the user's name.  Generally if the user is already logged in, you can just include the config.php script and then access the user's login name using $_SESSION["login"].
Logged

~ Jez
wintallo
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


View Profile WWW Email
« Reply #2 on: December 04, 2006, 01:56:35 am »

Sorry,

I was unclear on my meaning. The problem is, it doesn't work. I tested it by throwing in an echo $currentuser; and it didn't work. The variable comes up blank.

Sorry for the confusion.

Joel

btw, cool avatar  Cheesy  
« Last Edit: December 04, 2006, 01:57:57 am by wintallo » Logged

~~ Joel Verhag[c
munk
Administrator
Sr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 368


View Profile WWW
« Reply #3 on: December 05, 2006, 02:43:02 pm »

Mmm that's odd then.  You can try debugging it by using:

Code:
var_dump($_SESSION);

somewhere after you include the config.php file, that should allow you to see what's contained in $_SESSION.  The odd thing is if you got past the checkLoggedIn() part ok, the session variables should be set ok otherwise you'd be redirected to the login page, so $_SESSION["login"] should be set already.  See what the contents of $_SESSION are using var_dump, hopefully that will clear things up.
Logged

~ Jez
andyb
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 2


View Profile Email
« Reply #4 on: August 07, 2008, 03:17:13 pm »

Sorry to bring up an old thread, but I'm having the exact same problem.

But, in detail:

The login file has become the index.php.  Under this, the "members" page is menu.php.   From there, the user can select one of 3 options, 2 sub pages, or logout.  The two subpages contain forms, where the user enters data and upon "Submit" it loads another PHP file which generates a PDF file.

The problem is that once the user hits the file to generate the PDF, all session data is lost.  No login, no nothing.  I can't put the checklogin function in the top of the file, as it just gives fatal errors. 

The PDF stuff is a PHP file, with some add-in PDF library I found, called FPDF.

I've spent the last 4 hours playing around, and can't get anything to work.  Any ideas, or can someone point me in the right direction?

Can post code/output if necessary.

Edit:  I did a var_dump($_SESSION) on each page, and it all works good except on the pages I mentioned where it is problematic, where it returns NULL.    I also set up an additional subpage, on the same level as the problem pages, with just a var_dump and it still comes back NULL.   
« Last Edit: August 07, 2008, 03:24:34 pm by andyb » Logged
munk
Administrator
Sr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 368


View Profile WWW
« Reply #5 on: August 07, 2008, 04:12:56 pm »

You should be able to add the session functionality in your PDF creation php script, just make sure it's all done before any of the PDF creation stuff.

It should be enough to just have session_start() at the top of the script and the session variables should get read in properly, is that not working?

Can you paste an outline of the code maybe?
Logged

~ Jez
andyb
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 2


View Profile Email
« Reply #6 on: August 08, 2008, 12:52:40 pm »

It should be enough to just have session_start() at the top of the script and the session variables should get read in properly, is that not working?

I'll have a play around and if I still have dramas, I'll post.

Thanks!
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!