PHP Require makes my page blank? - php

I have a reporting website that I created and I'm slowly adding functionality to it. I've just added a part where it is supposed to force a user to log in. It's really just to capture the users CorpID, I don't keep or record the password and it's not required.
Right now I have the login portion working. Then I'm trying to run a check to make sure that a user is logged in and if not to force them to log in. I am only doing this right now on the Admin page, which only I have access to. Here is how I've got it right now:
AdminPage.php:
<body>
<?php
require 'CheckLogin.php';
include 'Menu.php';
?>
Other code for the page
CheckLogin.php:
<?php
$Expiration = time() - (60*60*24*7);
echo "You made it to here!";
if(!isset($_COOKIE['UserName']))
{
if(isset($_POST['UserName']))
{
setcookie("UserName",$_POST['UserName'],$Expiration);
}
else
{
echo "<script>location.href='LoginForm.php'</script>";
}
}
else
{
if(isset($_POST['UserName']))
{
setcookie("UserName",$_POST['UserName'],$Expiration);
}
else
{
setcookie("UserName",$_COOKIE['UserName'],$Expiration);
}
}
?>
UPDATE
Here's the Menu.php
<?php
$AdminUsers = include 'AdminUsernames.php';
if(isset($_COOKIE['UserName']) && in_array($_COOKIE['UserName'],$AdminUsers,TRUE))
{
$user = 'Admin';
}
else
{
$user = 'User';
}
//echo "<BR>"; print_r($_COOKIE['UserName']);
//echo "<BR>"; print_r($AdminUsers);
?>
<div class="menu-wrap">
<nav class="menu">
<ul class="clearfix" id="menu">
<li>Home</li>
<li>Report Builder</li>
<li>OPCEN Reports
<ul class="sub-menu">
<li>New COEI OPR Report</li>
<li>New OSP OPR Report</li>
<li>EOJ Report</li>
<li>Material Tracking</li>
<li>Vendor Material Tracking</li>
<li>CAF2 Tracker</li>
<li>JIM Report</li>
</ul>
</li>
<li>CAFII Reports
<ul class="sub-menu">
<li class="minHeight">Material Received Job Not Started</li>
<li class="minHeight">CAF2 Tracker New Test</li>
<?php
include 'DBConn.php';
$data = $conn->prepare('SELECT Id, QName, SSRSName from pmdb.QDefs where QSrc = 2 AND IsActive = 1 order by QName');
$data->execute();
$result = $data->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $q)
{
echo '<li class="minHeight">' . $q['QName'] . '</li>';
}
?>
</ul>
</li>
<li>Invoicing/Closing
<ul class="sub-menu">
<li>Non-Varasset Invoices</li>
</ul>
</li>
<li>ENG Reports
<ul class="sub-menu">
<li>Approved Projects</li>
<li>Approved Projects Previous Day</li>
<li>M6Action</li>
</ul>
</li>
<?php
if($user == 'Admin')
{
include 'AdminMenu.php';
}
?>
</ul>
</nav>
</div>
It's really just a standard menu and has been working fine till I added the require for the CheckLogin.php page.
All I get is a blank page when I have this require in the AdminPage.php. I don't get the echo I don't get the menu or anything.
What am I doing wrong? This isn't the first time that I've used the require, but it is the first time that it results in a blank page.
I do know that I have the expiration set to last week, I'm trying to force a re-login.

Put the following in your script before any includes or requires.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Using Doug's suggestion from above I found the error. In using the require I did not use the full path for the file and so it could not be opened. What that line should look like is:
require 'Helper/LoginCheck.php';

Related

Nav bar appears twice doesn't respect php if statement

I am trying to change my navigation bar depending on whether the user has signed in. Eventually also checking whether the user is an admin or not and having different list items depending on these conditions.
The issue is since my condition is in PHP it doesn't seem to respect the if statement and when the page is loaded both ul are shown.
I have a session start at the top of my page so the value of "loggedin" should be stored.
<header>
<div class="container">
<div class="Logo">
<img src="./images/LogoSmall.png" width="60px" height="60px" alt="Logo">
<h2> Quality Speakers Global </h2>
</div>
<div>
<nav>
<?php
if($_SESSION["loggedin"] == "yes"){
?>
<ul>
<li class="current">Home</li>
<li>Login/Register</li>
<li>Products</li>
<li>Report</li>
<li>My Account</li>
</ul>
<?php } else{?>
<ul>
<li class="current">Home</li>
<li>Login/Register</li>
<li>Products</li>
<li>Report</li>
</ul>
<?php
}
?>
</nav>
</div>
</header>
What i want is to only show the ul depending on the condition
here is my php login code:
<?php
session_start();
$con = mysqli_connect('localhost','root','pass1','accounts');
if(isset($_POST["UsernameLogin"])) {
$Logusername = $_POST['UsernameLogin'];
}
if(isset($_POST["PasswordLogin"])) {
$Logpassword = $_POST['PasswordLogin'];
}
$query = " select * from users where username = '$Logusername' && password = '$Logpassword'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
$num = mysqli_num_rows($result);
if($num == 1) {
$_SESSION["username"] = $Logusername;
$_SESSION["level"] = $row["usertype"];
$_SESSION["loggedin"] = 'yes';
if($_SESSION["level"] == "admin") {
header('location:AccountsPage.php');
} else {
header('location:userpage.php');
}
} else {
header("Location: Index.html");
}
?>
Your index.html page is not being parsed as php. Change the extension to .php or change configuration to also parse html files.

How to retrieve all but Renew Account for a system admin

Please, I'm new to PHP and I'm building a subscription-based ecommerce site. I've been able to customize the pages such that they look different based on who's browsing what, but I'd like to take the Renew Account off the admin page since they don't have to subscribe.
I've been struggling all day. Please, could someone show me how it could be achieved?
Below is the snippet:
<?php // Show the user info or the login form:
if (isset($_SESSION['user_id'])) {
// Show basic user options:
echo '<div class="title">
<h4>Manage Your Account</h4>
</div>
<ul>
<li>Renew Account</li>
<li>Change Password</li>
<li>Favorites</li>
<li>History</li>
<li>Recommendations</li>
<li>Logout</li>
</ul>
';
// Show admin options, if appropriate:
if (isset($_SESSION['user_admin'])) {
echo '<div class="title">
<h4>Administration</h4>
</div>
<ul>
<li>Add Page</li>
<li>Add PDF</li>
<li>Blah</li>
</ul>
';
}
} else { // Show the login form:
require ('login_form.inc.php');
}
?>
What you could do is to first check if the user admin session is set, to which I added the same conditional value for in the first conditional statement.
If it is set, then assign an empty value for what I named as $renew, with an else{} with the value that I removed from your existing <li></li>.
The first two session arrays here are only representational values of course.
I concatenated the '.$renew.' variable in the menu.
Note: Make sure that the session was started using session_start() inside all pages using sessions; that is not known.
$_SESSION['user_id'] = 123;
$_SESSION['user_admin'] = "john";
if (isset($_SESSION['user_admin'])) {
$renew = '';
} else {
$renew = '<li>Renew Account</li>';
}
if (isset($_SESSION['user_id'])) {
// Show basic user options:
echo '<div class="title">
<h4>Manage Your Account</h4>
</div>
<ul>
'.$renew.'
<li>Change Password</li>
<li>Favorites</li>
<li>History</li>
<li>Recommendations</li>
<li>Logout</li>
</ul>
';
// Show admin options, if appropriate:
if (isset($_SESSION['user_admin'])) {
echo '<div class="title">
<h4>Administration</h4>
</div>
<ul>
<li>Add Page</li>
<li>Add PDF</li>
<li>Blah</li>
</ul>
';
}
}
Try..
If(!isset($_SESSION['user_admin'])) {echo '<li>Renew Account</li>'; }
This way if it the session variable for admin is set the echo won't occur.

unable to change login menu to logout after user logs in PHP

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();

Active navigation bar in php

I have made a menu bar in a separate file and included it all pages. I am trying to achieve a good active php code to make my navigation bar active when I am on the page even on the dropdown. Most of the time I do get errors using the $_GET method. If anyone could help me with a simple active function using php that would be great.
Page: nav.php
if ($_GET['table'] != 'users')
{ $page1 = 'none'; } else { $page1 = 'active'; }
if ($_GET['table'] != 'carrier_exceptions')
{ $page2 = 'none'; } else { $page2 = 'active'; }.............
<nav>
<ul>
<li>Users</li>
<li class="dropdown">Carrier Exceptions
<div class="dropdown-content">
Carrier Costing
Carrier Code
Carrier Service Code
</div>
</li>
<li>Markup Percent</li>
<li>Service Code</li>
<li>Snap Activity Pricing</li>
<li>Vat</li>
<li>Country Code</li>
<li style="float:right">Logout
</ul>
</nav>

How to show username between html with PHP

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.

Categories