PHP script only shows when admin is loggedin

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
When people log into my site up at the top right shows their profile picture, a link to their account and a sign out link (regular user). But whenever people log in the top right is invisible, even on the source. Only the admin and not logged in users (register & log-in) can see it.

PHP:
<?php
$name = (isset($_SESSION['loggedin'])) ? $_SESSION['loggedin'] : '0';
if($name == '0') {
    echo "<a href=\"index.php?p=signInForm\">Log in</a>&nbsp;\n";
    echo "<a href=\"index.php?p=registerForm\">Register</a>\n";
} else {
    $sth = $dbh->query("SELECT id,username,avatar,admin FROM users WHERE username = '$name'");        
    
    while($row=$sth->fetch()){
        $userID = $row['id'];
        $username = $row['username'];
        $avatar = $row['avatar'];
        $admin = $row['admin'];
        
        echo "<a href=\"http://x10hosting.com/forums/images/userImages/avatars/$avatar\"><img src=\"http://x10hosting.com/forums/images/userImages/avatars/$avatar\" width='50' height='50' align='left' /></a><br />\n";
        echo "Welcome $username<br />\n";
        echo "<a href=\"index.php?p=viewProfile&id=$userID\">My profile page</a><br />\n";
        echo "<a href=\"index.php?p=signOutFinish\">Sign Out</a>\n";
if($admin == 1) {
        echo "<a href=\"index.php?p=admin/admin_checkentries\">Administration</a>";
    }
}
}
?>

I have no idea what is wrong
 
Last edited:

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
Yes that is in my index page in which I include this page.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Well, you are assuming that the value for $_SESSION['loggedin'] is a valid username, ie in the database. If your query returns 0 rows, you get no output.

Try echoing its value to the page. Perhaps your code that supposed to set it when they log in is faulty. Maybe you misspelled 'loggedin' there, or something else.
 
Last edited:

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
Nope, it returns the value I want it. But it does not do what I want with the value... Which confuses me
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP:
<?php
$name = (isset($_SESSION['loggedin'])) ? $_SESSION['loggedin'] : '0';
if($name == '0') {   #### $name not '0', otherwise this would be in source
    echo "<a href=\"index.php?p=signInForm\">Log in</a>&nbsp;\n";
    echo "<a href=\"index.php?p=registerForm\">Register</a>\n";
} else {
    $sth = $dbh->query("SELECT id,username,avatar,admin FROM users WHERE username = '$name'");     
    ####   
    #### query should be ok, otherwise next line should cause an error, 
    #### since $sth would be false, not an object
    ####
    while($row=$sth->fetch()){   
        ##
        ## if you got at least one row, you would get something printed.
        ##  ergo, your request returned 0 rows
        ##  
        $userID = $row['id'];
        $username = $row['username'];
        $avatar = $row['avatar'];
        $admin = $row['admin'];
        
        echo "<a href=\"http://x10hosting.com/forums/images/userImages/avatars/$avatar\"><img src=\"http://x10hosting.com/forums/images/userImages/avatars/$avatar\" width='50' height='50' align='left' /></a><br />\n";
        echo "Welcome $username<br />\n";
        echo "<a href=\"index.php?p=viewProfile&id=$userID\">My profile page</a><br />\n";
        echo "<a href=\"index.php?p=signOutFinish\">Sign Out</a>\n";
if($admin == 1) {
        echo "<a href=\"index.php?p=admin/admin_checkentries\">Administration</a>";
    }
}
}
?>
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
I didn't. I added comments.

The only way for nothing to show up is for $name to be set to a username that is not in the database.
If it is set to '0' you get the Sign In links.
So it cannot be '0'
So it contains the contents of $_SESSION['loggedin']
If your query returned an error, you should get an error message. You do not. So, no error.
If the query returns any rows, something is printed.
Nothing is printed.
Hence, your query returns 0 rows--except when it is an admin.
So, when a regular user logs in, it appears that $_SESSION['loggedin'] is not set to the proper value, but when an admin signs in, it does.
Or your regular users are not actually in the db.

Edit/Add: you might want to use phpMyAdmin to view the contents of that table.
 
Last edited:

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
Waiting for server to come back online to see if your solution worked.
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
Edit: Everything checks out and it should work. Please help?

Edit 2: It must have been the server move because it works fine now.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Explain exactly what happens when
1) logged admin sees the page
2) logged user sees the page
3) guest sees the page
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
Admin: Sees profile picture, sign-out and admin page
User: Sees profile picture and sign-out
Guest: Sees Log-in and Register

It works now though.
 
Top