Jump to content

Archived

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

benji87

Session Images

Recommended Posts

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:

[b]checkuser.php (registering variable):[/b]

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

[b]login_success.php:[/b]

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

Share this post


Link to post
Share on other sites
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)):
[code]<?php
session_start();  // always should be the first executable line in each script where you use sessions
$_SESSION['loggedin'] = 0;
?>[/code]
login_success.php:
[code]<?php
session_start();
if($_SESSION['loggedin'] == 0)
       echo '<img src=/userimgs/ben.gif>';
else
       echo '<img src=/userimgs/standard.gif>';
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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:

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

user text code:

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

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

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.