I'm creating a Server Side php Website application.
In my first page , the whole code is php , and I've set
$_SESSION["lang"] = 'lang.en.php';
$_SESSION["lang"] = 'lang.it.php';
When I change flag, I change a variable :
if (Trim($_SESSION["lang"])=='')
{
include 'lang.it.php';
}
if (Trim($_SESSION["lang"])=='lang.en.php')
{
include 'lang.en.php';
}
if (Trim($_SESSION["lang"])=='lang.it.php')
{
include 'lang.it.php';
}
I have the following problem
My second page .php is 50% html and 50% php because in html I put a simple dropdown menu.
My Question is : How can I pass to html my variables from another php page without using Jquery?
<li> <a class="active" href="home.php"><?php $home?></a> </li>
Variable $Home is stored in - lang.it.php -
How di I access to my variables?
P.s.
I don't want to pass all Menu's variable in the session because I think is crazy to do..
EDIT --
html
<head>
<link href="Css/Menu.css" rel="stylesheet" type="text/css">
<script>
</head>
<body>
<center ><div id="header"><center >
<ul>
<li><a class="active" href="home.php">Home</a></li>
<li>Area Download</li>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn"
onclick="myFunction()">MenĂ¹</a>
<div class="dropdown-content" id="myDropdown">
VARIABLE 1 HERE
VARIABLE 2 HERE
VARIABLE 3 HERE
</div>
<li>Logout</li>
</li>
</ul>
</div>
<center ><div id="Realbody"><center >
<center ><div id="Footer"><center >
</html>
<?php
require_once("rpcl/rpcl.inc.php");
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");
use_unit("styles.inc.php");
include 'parameters.php';
//Class definition
class PaginaDownload extends Page
{
public $Label1 = null;
public $LoginCss = null;
function PaginaDownloadShow($sender, $params)
{
if ((empty($_SESSION["name"])) || (empty($_SESSION["user"])))
{
header('Location: \index.php');
}
}
}
global $application;
global $PaginaDownload;
//Creates the form
$PaginaDownload=new PaginaDownload($application);
//Read from resource file
$PaginaDownload->loadResource(__FILE__);
//Shows the form
$PaginaDownload->show();
?>
I've to replace VARIABLE 1 , 2 , 3 with my variable on Lang.php
<li><a class="active" href="home.php">Home</a></li>
<li><a class="active" href="home.php"><?php $home?> </a></li>
That's my try :
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");
use_unit("styles.inc.php");
use_unit("Zend/zauth.inc.php");
include '/parameters.php';
if (Trim($_SESSION["lang"])=='') { include 'lang.it.php'; }
else
include Trim($_SESSION["lang"]);
//Class definition
and here my lang file
<?php
/*
------------------
Language: English
------------------
*/
//Login Page #1/X
$title_page_Login = 'Login';
$Err_1_Login = 'Login Credentials Incorrect';
$Err_2_Login = 'Not Found User :';
$Err_3_Login = 'Database Offline';
$Err_4_Login = 'Insert User & Password';
$Err_5_Login = 'Need User';
$Err_6_Login = 'Need Password';
$req_user = 'Insert Username';
$req_pass = 'Insert Password';
$Bt_Label = 'Sign In';
//-------------------
//Home
$home = 'Home';
?>
Related
My navigation is called on all page with code <?php include 'navigation.php'; ?>. Navigation I want to use on all pages:
<nav class="white-color">
<!-- nav code -->
</nav>
But I need to change class="white-color" on specific pages to be class="black-color". Is there a option to add simple PHP code to define:
<?php
if($page == "index") {
echo '
<nav class="first-class">
</nav>
';
} else if($page == "register") {
echo '
<nav class="second-class">
</nav>
';
} else {
/* DEFAULT CLASS FOR PAGES*/
echo '
<nav class="third-class">
</nav>
';
}
?>
Or maybe I can resolve this with the switch method. Any advice or example will be good.
Thanks all for helping me.
basename($_SERVER['SCRIPT_FILENAME'])
will return the file name that is currently in use. So if you are on localhost/index.php it will return index.php.
Furthermore you can make your code more effective by doing the following:
print "<nav class='";
if(basename($_SERVER['SCRIPT_FILENAME']) == "index.php"){
print "first-class";
}
else if(basename($_SERVER['SCRIPT_FILENAME']) == "register.php"){
print "second-class";
}
else{
print "third-class";
}
print "'></nav>";
If we take into account the comment from deceze it would look like this.
if(basename($_SERVER['SCRIPT_FILENAME']) == "index.php"){
$navClass = "first-class";
}
else if(basename($_SERVER['SCRIPT_FILENAME']) == "register.php"){
$navClass = "second-class";
}
else{
$navClass = "third-class";
}
print "<nav class='$navClass'></nav>";
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.
I am changing the color of a link after it is selected. The color is changing, but when the selected page is rendered it returns to the default color.
php pages look like this:
<!DOCTYPE html>
<html>
<?php
$Page = "Contact";
include 'header.php';
?>
<body>
<?php
include "facebook.php";
?>
<?php
include "headers.php";
?>
<?php
include "navBar.php";
?>
<?php
include "containers.php";
?>
<?php
include "footer.php";
?>
</body>
</html>
navigation bar where selection is made:
<div class="navBar">
<a class="aNavBar" href="index.php">Home</a> <a class="aNavBar" href="about.php">About</a> <a class="aNavBar" href="galleries.php">Gallery</a> <a class="aNavBar" href="equipment.php">Equipment</a> <a class="aNavBar" href="links.php">Links</a> <a class="aNavBar" href="contact.php">Contact</a>
</div>
<script>
$(document).ready(function()
{
$('.navBar a').click(function()
{
var href = $(this).attr('href'); //location.href;
alert(href);
$(this).addClass('selected');
});
});
</script>
I've tried putting code in the main (contact).php file, but the same thing is occurring.
You can also do this in following way
$path = $_SERVER['PHP_SELF']; // will return http://test.com/index.php for our example
$page = basename($path); // will return index.php
Then put condition
<a class=<?php ($page == index.php) ? echo "aNavBar selected" : echo "aNavBar"; ?> href="index.php">Home</a>
The links defined in html have standard colors, even when they are clicked or not. What defines if a link was clicked is the browser cache. As you are verified if it was selected, when the page loads this information will not be kept. It may be interesting to save the click of this link in the user's history, in a database, because through the browser it will not be possible.
Simply put, page state is not persisted after the page reloads or changes. So, you should not expect the selected hyperlink to remain 'selected' when the 2nd page is loaded. You can do it programatically though:
Something like this:
<div class="navBar">
<a class="aNavBar<?php if(strpos($_SERVER['PHP_SELF'], 'index.php')!==false) echo ' selected'; ?>" href="index.php">Home</a> <a class="aNavBar" href="about.php<?php if(strpos($_SERVER['PHP_SELF'], 'about.php')!==false) echo ' selected'; ?>">About</a>
The following worked:
<?
$path = $_SERVER['PHP_SELF']; // will return http://test.com/index.php for our example
$page = basename($path); // will return index.php
?>
<a class="aNavBar" <? if($page == "index.php") echo "style='color:red'";?> href="index.php">Home</a>
Thanks for your help!
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';
This question already has answers here:
How can I get an unknown username given an ID?
(2 answers)
Closed 1 year ago.
i am new to this field and this is the first time i am working with session, the question may seem very basic but would appreciate if someone could help me. Currently I have made a login and logout page using session and wish to display data of the particular user who has logged in. The user is redirected to retailer_login.php after they sign in, apart from login form there are 4 pages for the entire login and logout process.
retailer_login.php, retailer_session.php, retailer_profile.php, retailer_logout.php
Every page is working fine however i am able to display only single data column of the user from database but i wish to display the entire information that is stored about that specific user.
DATABASE
Id name email password country city state occupation
1 sam sam#gmail.com sam XYZ ZBC QWE student
retailer_login page
<?php
session_start(); // Starting Session
if (isset($_POST['submit'])) {
try {
if (empty($_POST['email']) || empty($_POST['password'])) {
throw new Exception("email or Password is invalid");
} else {
// Define $email and $password
$email = $_POST['email'];
$password = $_POST['password'];
// To protect MySQL injection for Security purpose
$email = stripslashes($email);
$password = stripslashes($password);
$mail = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
//Etablishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("abc.com", "abc", "abc");
// Selecting Database
$db= mysql_select_db("abc", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from retailerregister where password='$password' AND email='$email'", $connection);
$rows = mysql_num_rows($query);
if ($rows != 1)
throw new Exception("email or Password is invalid");
$_SESSION['login_user'] = $email; // Initializing Session
header("location: retailer_profile.php"); // Redirecting To Other Page
mysql_close($connection); // Closing Connection
}
}
catch (Exception $e) {
$_SESSION['login_error'] = $e->getMessage();
header("Location: index.html");
}
}
?>
retailer_profile page
<?php
include('retailer_session.php');
?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome to your homepage</title>
<meta name="viewport" content="width=device-width", initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link href="css/styles.css" rel="stylesheet" />
<link href="css/carousel.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id="profile">
<div class="navbar navbar-inverse navbar-static-top">
<div class="container">
<id="welcome">Welcome : <i><?php echo $login_session; ?></i>
<button class = "navbar-toggle" data-toggle = "collapse" data-target = ".navHeaderCollapse">
<span class = "icon-bar"> </span>
<span class = "icon-bar"> </span>
<span class = "icon-bar"> </span>
</button>
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class = "nav navbar-nav navbar-right">
<li class ="active"> Home</li>
<li> Profile</li>
<li class="dropdown">
Property <b class ="caret"></b>
<ul class="dropdown-menu">
<li> Add property </li>
<li> View property </li>
</ul>
</li>
<li> <id="logout">Log Out</li>
</ul>
</div>
</div>
</div>
</div>
<div name="container">
</div>
<script src = "js/jquery-1.11.1.js"> </script>
<script src = "js/bootstrap.js"> </script>
</body>
</html>
retailer_logout page
<?php
session_start();
if(session_destroy()) // Destroying All Sessions
{
header("Location: index.html"); // Redirecting To Home Page
}
?>
retailer_session page
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("abc.com", "abc", "abc");
// Selecting Database
$db = mysql_select_db("abc", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select * from retailerregister where email='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['email'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: index.html'); // Redirecting To Home Page
}
?>
right now i am only able to use $login_session in order to display email on profile page. Can anyone please tell my how to display other data of the logged in user on the retailer_profile page through session
Just create another variables about current logged in user:
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['email'];
// another user data
$user_name = $row['name'];
$user_country = $row['country'];
$user_city = $row['city'];
$user_state = $row['state'];
$user_occupation = $row['occupation'];
Or you can just use one variable which shouldn't be overwritten:
$user_data = $row;
And then somewhere in script:
echo $user_data['city']; // etc...