I have a LAMP(Ubuntu) Server running on a physical machine. My goal is to host a website on the server. Right now, I have all my files in my /var/www/html directory and inside certain HTML files, I run PHP scripts to check if the user is logged in or not such as below:
<div class="collapse navbar-collapse" id = "navCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active link-nav">Home</li>
<li class="link-nav">Categories</li>
<li class="link-nav">About</li>
<!-- This part -->
<?php
if($_SESSION['loggedin'] != 1){
echo '<li class="link-nav">
Log In
</li>
<li class="link-nav">
Register
</li>';
}else{
echo "<li class = 'link-nav'> Log Out ";
}
?>
<!-- End of PHP -->
</ul>
</div>
When I open up to my server in the browser, all I get is this:
The $_SESSION['loggedin'] variable has already been set but both the Login and Logout buttons show. Also, the text in between the two sets of strings is showing on the page.
When I display this on my local computer using XAMPP, this doesn't happen.
Any ideas on how to fix this?
Thanks in advance!
This line is invalid. With proper error reporting on, you would see the error. When testing, it will help a lot to have error reporting turned on so you can see the helpful errors :)
echo "<li class = 'link-nav'> Log Out ";
You can not use the double quotes inside themselves without escaping them!
Instead write it like this:
echo '<li class="link-nav">Log Out';
See if that works.
Try this, It's much easier to work with code that has islands rather than everything being a PHP string(Also, take a look at http://www.Laravel.com and http://www.Laracasts.com):
<?php if($_SESSION['loggedin'] != 1){ ?>
<li class="link-nav">
Log In
</li>
<li class="link-nav">
Register
</li>
<?php }else{ ?>
<li class="link-nav"> Log Out
<?php } ?>
Related
I am trying to build authentication syatem in my website and i am able to successfully register and login but after login my menu bar is still showing me option for login.
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li>HOME</li>
<li>REGISTRATION</li>
<li>DOWNLOAD</li>
<li>CONTACT</li>
<?php
if(isset($_SESSION['user_id']) === true){
?><li>LOGOUT</li><?php
}else{
?><li>LOGIN</li><?php
}
?>
</ul>
</div>
</div>
what should i do?
this piece will fail every time
if(isset($_SESSION['user_id']) === true){
?><li>LOGOUT</li><?php
}else{
?><li>LOGIN</li><?php
?>
normally we check for session like this , check if it is set and check if it not empty and also modified your echo way
if(isset($_SESSION['user_id']) ) && (!empty($_SESSION['user_id']) ){
echo '<li>LOGOUT</li>';
}else{
echo '<li>LOGIN</li>';
}
?>
also very important at the top of each page use this code at line 1 and 2 otherwise no session functions will work
<?php
session_start();
Hi i have a script in which there are bootstrap dropdowns. The code for it is as follows:-
<ul class="nav navbar-nav navbar-right">
<li class="dropdown dropdown-user" style="font-size:15px;">
<a class="dropdown-toggle" data-toggle="dropdown">
<i class="icon-user"></i> <span><?php echo $name; ?></span> <i class="caret"></i></a>
<ul class="dropdown-menu dropdown-menu-right width-220">
<li><i class="icon-lock2"></i> Change Password</li>
<li><i class="icon-switch2"></i> Logout</li>
</ul>
</li>
</ul>
Now i run a php script after a form is submitted in which i am checking whether a table is there in the database or not. If it is there it fethces it and if it is not there php script dies. The code for it is below:-
<?php
if(isset($_POST['submit']))
{
$sql="SELECT * FROM table1";
$results = $con->query($sql);
if ($results === FALSE) {
echo "<script> noty({text: 'No Data Found',layout: 'topRight',timeout: 3500,closeWith: ['click', 'hover'],type: 'error'});</script>";
die();
} else {
........
}
Now whenever the php script dies the dropdowns stop working.
I am just starting to learn php so please let me know what I am doing wrong or if there is a better way of looking at this problem. Any help is highly appreciated.
Check if you included bootstrap js file at the end of your page.
So when PHP dies, The bootsrap will not be loaded to the page. If its the reason change die with something else or move bootstrap js file to header.
PS:Besides use die whenever something goes wrong is not the best idea to handle errors.
My issue is: I have a login system and I wanna show the current username in the page.
calendar.php:
// SOME CODE
<ul class="nav navbar-nav">
<li>Welcome <? echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
// SOME CODE
This shows the "Welcome" but don't show username...
How can I do this? Thank you all in advance.
Make sure $_SESSION['username'] is already set, i.e:
index.php
<?php
session_start();
$_SESSION['username'] = "someusername";
calendar.php
<?php
session_start();
?>
<ul class="nav navbar-nav">
<li>Welcome <?php echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
Note:
Ensure session_start(); is the first statement on the script, i.e.:
<?php
session_start();
Otherwise you may get the error:
"Headers already sent..."
Learn how to use php sessions across multiple files here
Unless you are using a rather old system, you need
<li>Welcome <?php echo $_SESSION['username'] ?></li>
If it still doesn't show, ensure that the page is being parsed for PHP. Then, make sure that $_SESSION['username'] has a value.
You forgot to write php in your code:--
<ul class="nav navbar-nav">
<li>Welcome <?php echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
Note:-
1.This code file name will be .php not .html and if your Session have username index and have some value then only it will show.
2.As you asked for the error you need to put this code on very upside of your php page
<?php
session_start();
$_SESSION['username'] = 'any user name';
After that my above code and then everything works fine.
3.If you want this session value to any other page on that page write first session_start(); on top and then you will get the value.
I am trying to use the php include tag for my header and footer, and can't seem to find the problem. My include code is fine in my index file, and my header.php file shows no errors, but t doesn't include the file when I preview it in a browser. Is this an easy fix or am I missing something.
My index.php file include snippet
<div id="container">
<?php include('include/header.php');?>
<div id="body">
My header.php file code
<?php
echo <<<END
<header>
<nav>
<li onclick="location.href='index.html';" style="cursor:pointer;">Home</li>
<li onclick="location.href='#';" style="cursor:pointer;">Contact</li>
<li onclick="location.href='#';" style="cursor:pointer;">FAQ's</li>
<li onclick="location.href='#';" style="cursor:pointer;">Product Solutions</li>
<li onclick="location.href='#';" style="cursor:pointer;">Services</li>
<li onclick="location.href='#';" style="cursor:pointer;">Request a Quote</li>
</nav>
</header>
END;
?>
It seems ok on my computer. Are you sure that's your header file is the right directory? It should be:
---- index.php
---- include (directory)
|---- header.php
If it's ok you must file permissions and be sure that index can include the header file.
Why do you need to use heredoc syntax?
Why not do this instead:
<?php
... some code ...
?>
<header>
<nav>
<li onclick="location.href='index.html';" style="cursor:pointer;">Home</li>
<li onclick="location.href='#';" style="cursor:pointer;">Contact</li>
<li onclick="location.href='#';" style="cursor:pointer;">FAQ's</li>
<li onclick="location.href='#';" style="cursor:pointer;">Product Solutions</li>
<li onclick="location.href='#';" style="cursor:pointer;">Services</li>
<li onclick="location.href='#';" style="cursor:pointer;">Request a Quote</li>
</nav>
</header>
<?php ... more code ... ?>
<!-- more html -->
You don't need echo. Any HTML not within tags are automatically outputted anyways. Remember that PHP is based on HTML. You can think of PHP as HTML with <?php?> tags mixed in.
Change this line:
<li onclick="location.href='#';" style="cursor:pointer;">FAQ's</li>
With this:
<li onclick="location.href='#';" style="cursor:pointer;">FAQ\'s</li>
The error was this:
FAQ's
And I changed it to this:
FAQ\'s
what do you mean by
echo <<<END ???
however the file header.php is in a separate folder ? look at in the console to see if it can locate the file or not... for me i used to work with :
require_once("include/header.php");
I'm building a wordpress theme. In the backend, the user has the option to enter the url for their social networks (i.e. twitter, facebook, instagram etc). These URL's are then dynamically added to images in the theme front end linking to the respective networks.
The issue I have is that if a user doesn't enter a url for a network, I don't want the image to display. I am trying to write code that says, if the url is blank, echo 'class="hidden"' - this class has display:none in the css.
here is a snippet of my php:
<ul class="icons">
<li <?php if (get_theme_mod('footer_twitter')==0) {echo 'class="hidden"'; } ?>><span class="label">Twitter</span></li>
</ul>
and the css:
ul.icons li.hidden {
display:none;
}
The php code above currently outputs the echo statement for all cases, even when a url is entered in the backend. Can anyone help me with this code
Check the return of "get_theme_mod()" You can check this by using, cause i dont think it "== 0". http://codex.wordpress.org/Function_Reference/get_theme_mod
var_dump(get_theme_mod('footer_twitter'));
//string(0)
Here is your new code:
<ul class="icons">
<li class="<?php echo empty(get_theme_mod('footer_twitter')) ? 'hidden' : ''; ?>">
<a href="<?php echo get_theme_mod('footer_twitter'); ?> " class="icon circle fa-twitter">
<span class="label">Twitter</span>
</a>
</li>
</ul>
Please check this Syntax: http://php.net/manual/en/control-structures.alternative-syntax.php its the best way to code control structures in your "View" code.
<?php if (!empty (get_theme_mod( 'set_address2' ))) {
echo get_theme_mod( 'set_address2');
}?>
This seemed to work for me in Wordpress.
Let me know if this works.