I have a div that contains a slider when the homepage of the website is opened. What im trying to achieve is that when the website is opened for the first time, the slider should appear. However, if the user goes another page other than the homepage and then returns to the homepage again, the slider should not appear.
Below is the code I am trying to implement:
<div class="homeslidermain" style="display:<?php echo empty($_SESSION['first_load']) ? 'block' : 'none'; ?>">
<?php putRevSlider("typewriter-effect", "homepage") ?>
</div>
The recommended way would be to set a cookie using setcookie() and getcookie() (http://php.net/manual/de/features.cookies.php).
If you want to use the session then you are setting "first_load" incorrectly. Make sure that on any page call:
session_start(); // before you do anything else
if(!isset($_SESSION['first_load'])) // set it to true on first load
... and to false in any other case.
The only reason why this might go wrong is if you are reinitializing your session wrong. Make sure you are still in the same session after switching pages.
There is no need to output the div as display:none. Just output the div only when user visits the homepage for the first time. Use the setcookie() function to remember that user already visited he homepage, but please note that you should call this function before any output.
<?php
if (empty($_COOKIE['homepage_visited'])) {
// Remember the first visit for one year
setcookie('homepage_visited', 1, strtotime('+1 year'));
// Show the slider
echo '<div class="homeslidermain">';
putRevSlider("typewriter-effect", "homepage");
echo '</div>';
}
There are several ways to achieve it, best is to check if user visiting page for first time
session_start();
if(!isset($_SESSION['first_load']))
{
$_SESSION['first_load'] = '1';
}
if(empty($_SESSION['first_load']))
{?>
<div>
Slider block // this block loads only is first load is empty
</div>
<?php
}?>
You Could try something like this
// start the session
session_start();
$bShowBanner = true;
if(isset($_SESSION['BannerShown'])){
$bShowBanner = false;
}else{
$_SESSION['BannerShown'] = true;
}
?>
<div class="homeslidermain" style="display:<?php echo ($bShowBanner ? 'block' : 'none'); ?>">
<?php putRevSlider("typewriter-effect", "homepage") ?>
</div>
Related
I am looking to show some content on a page based on the parameter in a link.
If a link is given to a user https://www.examplesite.com/example-page/?client_feeback=1
then they will see the content of the page, if not using the link, then users will not see the content.
Additionally, I need the users of the link to be able to look on other pages and return to the page where the content is hidden/shown and still see the content.
I have set a cookie in functions.php, that will expire in 30days.
code added into functions.php
add_action('init', 'set_feedback_cookie');
function set_feedback_cookie () {
$name = 'client_feedback';
$value= 1;
setcookie($name, $value, strtotime( '+30 days' ), "/example-page/", "examplesite.com", "true" );
}
I have then added the following into the example-page.php template file
<?php
if (!isset($_GET['client_feedback'])) { ?>
<style type="text/css">#form__feedback {display:none!important}</style>
<?php } else { ?>
<style type="text/css">#form__feedback {display:block!important}</style>
<?php } ?>
The cookie is loaded on to the site and the content is hidden/shown when using/ not-using the url link.
What is not working, is the ability to browse other pages on the site and come back to the page with the hidden content and still see it!
Since you confirmed in the comment section that you are not leaving your own domain, You could put the GET variable into a SESSION variable and do your checks based on that. That way, you will always have that available to you until the user leaves the site or you manually kill the session somewhere.
Example:
<?php
session_start();
$_SESSION["client_feedback"] = $_GET['client_feedback'];
?>
As long as declare your session_start(); at the very beginning of your page(s) (literally before ANYTHING else), you can access your session variable anywhere you want.
Now you can perform checks on your session variable with the logic you're trying to achieve in order to display your desired content.
Full Example:
<?php
session_start();
if($_GET['client_feedback'] != "") {
$_SESSION["client_feedback"] = $_GET['client_feedback'];
}
if( isset( $_SESSION["client_feedback"] ) != "" ) {
?> <style type="text/css">#form__feedback {display:block!important}</style> <?php
} else {
?> <style type="text/css">#form__feedback {display:none!important}</style> <?php
}
?>
You can read more about PHP sessions here.
I Have a button which can only be accessed if a manager logins and not an junior employee. I want to restrict the access of it. I am taking a SessionVariable to check but it doesn't seems to be working.
I have assigned access value 1 for a manager where I am checking Login credentials-
if ($_SESSION["AccessValue"] != 1)
{
header("Refresh:0; url=HomePage.php");
}
I believe you're looking for location:
if ($_SESSION['AccessValue'] != 1) {
header('Location: homePage.php');
}
see here: https://secure.php.net/manual/en/function.header.php
Though I'd much rather recommend hiding the button entirely from junior rather than preventing action of click.
e.g.
<?php if ($_SESSION['AccessValue'] == 1) : ?>
<button type="button" etc etc.>Click me</button>
<?php endif; ?>
You can disable the button itself for a Junior
if ($_SESSION["AccessValue"] != 1)
{
$disable_btn = 'disabled = "disabled"';
}else{
$disabled_btn= '';
}
HTML:
<button <?php echo $disabled_btn; ?> ></button>
You should set the cookie of session first using :
setcookie(session_id(),session_name(),time()+3600*72);
And for every time that you want access session you should start session using session_start();
Now for time that you specified (3600*72) the session will be able to access for that user and if she/he login the session will be accessible.
And after that you can use this session like a condition for echo the button tag or disable and enable that
if(isset($_SESSION('NAME'))
$status=$_SESSION('NAME');
<button ....<?php if ($status!=1)echo "disabled";?>.....>
button_name</button>
I have the following code
<?php
if($_SESSION['loggedin']){
echo '<li id="login-btn">Logout</li>';
}
else{
echo '<li id="login-btn">Login</li>';
}
?>
This is inside of the HTML for my Navbar. I want it to where if they are logged in, it will show "Logout", if they aren't logged in, it'll show "Login", (self explanatory)
I have this in my login.php
$loggedin = "";
$_SESSION['loggedin'] = true;
For some reason, no matter what I do, my navbar keeps displaying "Login"? Help please, thank you!
Session are global variables in php...
Session variables are not passed individually to each new page,
instead they are retrieved from the session we open at the beginning
of each page (session_start()).
if you want to access it on different page... you have to add
<?php
session_start();
?>
at the begining .... even in your login.php page
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)?
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.
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.
I have a dynamic link which fetches invoice detail based on invoice ID.
<a href='<?php echo $_SERVER['PHP_SELF']; ?>/retrieve?class=InvoiceLineItems&id=<?php echo $invoice['invoice_id']; ?>'><?php echo $invoice['invoice_number']; ?></a> <?php echo $invoice['customer_name'] ?> <?php echo $invoice['invoice_date'] ?>
It calls this function
public function retrieve($class,
$id = NULL)
{
switch ($class) {
case 'Invoice':
$invoices = $this->invoice->getInvoices();
include 'view/invoiceList.php';
break;
case 'InvoiceLineItems':
$partnerInfo = $this->partnerInfo->getPartnerInfo($id);
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
include 'view/invoice.php';
break;
}
}
However, the include statement found in case 'InvoiceLineItems:' appends the content of invoice.php to the bottom of the existing page rather than replacing it altogether. I've tried adding a target to the anchor, but that didn't work. How do I get the link to open the new page?
UPDATE: based on #sixeightzero suggestion, here is the call to retrieve();
if (isset($_REQUEST['id'])) {
// A request ID value indicates arrival here through link.
$this->retrieve('InvoiceLineItems',
$_REQUEST['id']);
}
Also, I tried using a header redirect.
ob_start();
header('Location: /view/invoice.php', 302);
ob_end_flush();
exit();
It redirects, but I lose access to my array variables from
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
So, I get errors like
Notice: Undefined variable: partnerInfo in C:\xampp\htdocs\bp\view\invoice.php on line 25
and
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\bp\view\invoice.php on line 25
put exit(); at the end of the function to stop executing code after calling it.
or better still use invoice.php to display the invoices instead of the current page.
1) Since, you are using super global *$_SERVER['PHP_SELF']*, the
anchor will always direct to the same page.
2) Now, I assume, you call the function retrieve() at the end of
the page which in turn, after you hit the link, appends
'view/invoice.php' to the page, so you see the appended content at the
end.
Now, you want to get the link to open the new page, there are two ways:
1) You redirect the page to the page view/invoice.php and there you
call the function retrieve and include target="self" in the anchor tag.
Or
2) You do what you are doing but include target="blank" in the
anchor tag.
In order to get the link to open as fresh content, do as follows:
1) Add javascript to the page. In the Html Head section add the
following lines:
<script type="text/javascript">
function hideMainContentDiv(){
document.getElementById("mainContent").setAttribute("style", "display:none");
}
</script>
2) Add the onclick attribute to the anchor tag as:
<a onclick="hideMainContentDiv()">
3) Now, enclose the content of the page in and give it
id="mainContent" as:
<div id="mainContent"> <!-- All your content --> </div>
4) At the end of the page when you have enclosed your content, add
another div and call your function retrieve as:
<div id="newContent">
<?php retrieve($class,$id); ?>
</div>
What this will do is:
When you click the link, it will call the javascript and hide all your existing page content in the div whose id="mainContent".
When your function retrieve() will be called, it will include 'view/invoice.php' which will show your new content.
Your old content has already been hidden by javascript call.
I hope this resolves your query.
The answer turned out to be sessions.
case 'InvoiceLineItems':
$partnerInfo = $this->partnerInfo->getPartnerInfo($id);
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
$_SESSION['partnerInfo'] = $partnerInfo;
$_SESSION['invoiceLineItems'] = $invoiceLineItems;
ob_start();
header('Location: /view/invoice.php');
ob_end_flush();
exit();
break;
I added the array data to two session variables in the controller. Then, in the view, I replaced the corresponding variable names -- $partnerInfo and $invoiceLineItems -- with their session equivalents $_SESSION['partnerInfo'] and $_SESSION['invoiceLineItems'].