Jump to content


Photo

Session Images


  • Please log in to reply
3 replies to this topic

#1 benji87

benji87
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationPortsmouth, UK

Posted 21 March 2006 - 10:44 PM

Hey all. Im currently working on a user section for my new site and im trying to define a user image using sessions but im having a little problem and dont know what to do next.

Here is my code so far:

checkuser.php (registering variable):

session_register('loggedin');
$_SESSION['loggedin'] = 0;

login_success.php:

<? if(loggedin == 0){
echo '<img src=/userimgs/ben.gif>';
}

else {
echo '<img src=/userimgs/standard.gif>';
}
?>

With the if statement i want to draw the users image url from the database if they are logged in but im not sure how to go about that i did try:

$query("SELECT userimg FROM mydatabase WHERE username='username'")
$result=mysql_query($query);

But i had no luck with that.

So i used the image source above just to test to see if the image would not appear if i was not logged in. But it appears even if im not logged in! I hope someone can guide me in the right direction coz i think im heading that way, i hope!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 March 2006 - 04:13 PM

Your programming style assumes that register_globals is enabled. This could be a false assumption, since the default has been "disabled" for at least 3 years. Do not use register_session(). Instead use the session variable directly.

Your code would then look like (checkuser.php (registering variable)):
<?php
session_start();  // always should be the first executable line in each script where you use sessions
$_SESSION['loggedin'] = 0;
?>
login_success.php:
<?php
session_start();
if($_SESSION['loggedin'] == 0)
       echo '<img src=/userimgs/ben.gif>';
else
       echo '<img src=/userimgs/standard.gif>';
?>

Ken

#3 benji87

benji87
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationPortsmouth, UK

Posted 23 March 2006 - 09:35 PM

Thanks that code worked great! But there is still one problem! Ive used the same method for the text that appears next to the image that welcomes the user but if you go onto my site and you are not logged in then the code wont skip to the else statement because im guessing it cant find the session variable. So how am i meant to get it to display the else statement without registering the varibles first? As my varibles are registered in the login process... Here is my code below:

user image code:

<?php
session_start();
if($_SESSION['loggedin'] == 0)
       echo $_SESSION['userimg'];
else
       echo '<img src=/userimgs/user.gif>';
?>

user text code:

<?php
session_start();
if($_SESSION['loggedin'] == 0)
       echo "Welcome ". $_SESSION['username'] ."<br>You are logged in.<br />";
else
       echo "Please login or register";
?>

heres my website: [a href=\"http://www.bubblebox-design.co.uk\" target=\"_blank\"]Studio2310[/a]

#4 benji87

benji87
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationPortsmouth, UK

Posted 27 March 2006 - 05:47 PM

bump




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users