+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: PHP script only shows when admin is loggedin

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    PHP script only shows when admin is loggedin

    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 Code:
    <?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 by as4s1n; 03-20-2010 at 05:31 PM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: PHP script only shows when admin is loggedin

    Got session_start() ?
    Nothing is always absolutely so.

  3. #3
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP script only shows when admin is loggedin

    Yes that is in my index page in which I include this page.
    Last edited by as4s1n; 03-20-2010 at 05:56 PM.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: PHP script only shows when admin is loggedin

    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 by descalzo; 03-20-2010 at 06:01 PM.
    Nothing is always absolutely so.

  5. #5
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP script only shows when admin is loggedin

    Nope, it returns the value I want it. But it does not do what I want with the value... Which confuses me

  6. #6
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: PHP script only shows when admin is loggedin

    PHP Code:
    <?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>";
        }
    }
    }
    ?>
    Nothing is always absolutely so.

  7. #7
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP script only shows when admin is loggedin

    I do not understand what you changed.

  8. #8
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: PHP script only shows when admin is loggedin

    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 by descalzo; 03-20-2010 at 11:55 PM.
    Nothing is always absolutely so.

  9. #9
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP script only shows when admin is loggedin

    So how can I fix this?

  10. #10
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP script only shows when admin is loggedin

    Waiting for server to come back online to see if your solution worked.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Why no site shows
    By deonduke in forum Free Hosting
    Replies: 5
    Last Post: 12-09-2009, 03:34 PM
  2. My website don´t shows (Index of /)
    By dagcapo in forum Free Hosting
    Replies: 2
    Last Post: 06-04-2009, 11:47 PM
  3. asp script shows instead of executing
    By starrynte in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 05-03-2009, 09:11 PM
  4. BUG on my acc. Cpanel shows up
    By aldylan in forum Free Hosting
    Replies: 4
    Last Post: 03-04-2008, 12:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers