In the header of the Home.html file, I use the $session function like this
<?php
@session_start();
if( $_SESSION['flag']==1)
echo'<script>window.location="login.html";</script>';
?>
While in the code above it only directs the user to the login file if they has not login yet.
What I want now is if the user has not login yet then they are still able to acess the home.html file however there are some content that I want to hide to the unregistered user.
I use this code to generate this:
$(document).ready(function(){
$check=1;
$("span").click(function(){
if($check==0)
{
$("#login").show();
$("#logout").hide();
}
else
{
$("#logout").show();
$("#login").hide();
}
});
});
So my question now, What I need to change in the header of the html file (in the session function) so that it can be combined with the $Check function?"
You have to combine php and javascript a bit like in the following example:
$(document).ready(function(){
var check = <?= $_SESSION['flag'] ?>;
$("span").click(function(){
if(check==0){
$("#login").show();
$("#logout").hide();
} else {
$("#logout").show();
$("#login").hide();
}
});
});
The shown example awaits $_SESSION['flag'] as integer value. Is $_SESSION['flag'] a string you have to set the check var in quotes.
Its not a good idea to do all this with javascript. All the user needs to do is view source
from the browser and they will see all the parts of the page that you want to hide.
Use PHP to decide what gets put in the page before you send it rather than hiding what gets sent.
Also
<?php
@session_start();
if( $_SESSION['flag']==1)
echo'<script>window.location="login.html";</script>';
?>
Is a bad way to do the page redirect, as it has to send the whole page to the browser just for the javascript to redirect the browser to another page. Better way:
<?php
session_start();
if( ! isset($_SESSION['flag']) || $_SESSION['flag'] == 1 ) {
header('Location: login.html');
exit;
}
?>
Now PHP just sends a header to the browser telling it to go to another page. Also you dont run the rest of this scritp for no reason.