Jump to content

Session Images


benji87

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!
Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.