In my website I set some values to session object like “user_status”, “user_name” and like so. The php file looks like this:
<script type="text/javascript">
var logged = <? echo $this->session->getValueOf("user_status"); ?>;
</script>
<a class="show_message" href="#">SHow my status</a>
Well, I have a js script that pretends do an action according to user status in the website, so, I have this:
$('.show_status').click(function(event){
//ask for user status
if (logged){
//do something
}
else{
//do another action for visitors
}
});
Walking around I thought if it is the best way flow data between session -> javascript, because if you inspect the page source at browser the value of user_status will be visible and could be riskable for website security.
Thanks in advance
EDIT:
loggedvar only takes a boolean value.- The js action must be executed each time the element
#(".show_status")is clicked.
If the JavaScript is just being used for interface stuff, and doesn’t have any back end effects, I probably wouldn’t worry too much about the insecurity of handling that logic client-side.
If security is an important thing though, I would recommend you use PHP to write the appropriate JavaScript function. For example:
On the page being viewed, perhaps in the header, you have:
In the file `logged_in_user_functions.js’ you have:
Meanwhile, in the file `visitor_functions.js’ you have:
Then you can add your logic into your page without having to check the user status. The proper behaviour is provided by virtue of which .js file was included:
This gives PHP (and thus the server, not the client) final say in what gets displayed to the user.