Pregunta

I want to enable or disable a div according to the session if it starts with an user or a guest. I want to do something like this:

First, i will evaluate if it is user or not by doing this:

<?php

    if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
    {
        $guest=true;
    } else {
        $guest=false;
    }

?>

then in jquery, i would like to say:

$('.box').click(function(){    // labBox appears when box is clicked
    if(<?php $guest?>)                  
    $("#LabBox").hide();
    else
    $("#LabBox").show();                            

});

Question: how can i use my php boolean var $guest to disable or hide some elements of my website? Do i have to do two distinct php files? one for users and other for guest (e.g, home.php and home_guest.php)?

¿Fue útil?

Solución

you could do the alternative such as

<script>
    var guest = '<?php echo $guest; ?>';
    $('.box').click(function(){    // labBox appears when box is clicked
        if(guest === "true") {             
            $("#LabBox").hide();
        } else {
            $("#LabBox").show();                            
        }
    });
</script>

This would simply allow you to pass the PHP value to a Javascript variable, in order for you to use it within the onClick.

Otros consejos

Remember: everything that reaches the client can be manipulated. Therefore, if you send an hidden element (say, an hidden <div>) any tech-savvy user can, and will, easily make them visible.

You MUST perform the checks about the login/guest status in your PHP script, and don't rely on jQuery to assemble the page at client side (hey, after all, the user may have disabled javascript altogether!)

You don't need two pages (eg: home.php and home_guest.php) to render different content based on the user level. Just use appropriately session/cookies and different echos.

Use a hidden input, populated by PHP, which jQuery can grab:

<?php
   echo "<input type=hidden id=guestcheck value=$guest/>"
?>

if ("#guestcheck").val()) {
}

I personally like this method because it allows me to check the source when debugging to find out where any errors may be (for instance you can plainly see in the source when viewing the page whether or not GUEST is true)

It depends on contents of those files. If the only difference is visibility of the block, it's more reasonable to do the check inline.

<?php if (isset($_SESSION['idUser'])) { ?>
    $('.box').click(function() { $("#LabBox").show(); }
<?php } ?>

Personally I would do it in the HTML rather than the JS file...

<?php

    if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
    {
        $loggedin=true;
    } else {
        $loggedin=false;
    }

?>

Then later on..

<?php if($loggedin===true){?>
    <div>User is logged in</div>
<?php }else{?>
    <div>Guest is viewing page</div>
<?php }?>

This means that the div for the user is not shown to the guest, whereas your currently solution only hides it from view (user could just use firebug/viewsource!

Why don't you just show/hide your div in the php depended on if they are a guest or not...

So...

<?php

if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
{
    $guest=true;
} else {
    $guest=false;
}

if($guest===true){
    echo "<div></div>";
}
else{
    //dont echo div
}

?>

PHP / server-side:

<?php
    if(!isset($_SESSION['idUser'])) // If it is Guest doesn't have id.
    {
        $guest=true;
    } else {
        $guest=false;
        // add #LabBox element from here to avoid junk/hidden elements for guests
    }
?>

JQuery / client-side:

$('.box').click(function(){    // labBox appears when box is clicked
    if (!<?php echo $guest?> && $('#LabBox').length > 0) {
        $('#LabBox').show();
    }
});

Then it is critical that any action requested by the user pass the "guest or not?" test before being granted from the server-side.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top