Call another php without leaving the defaults - php

I am a beginner in web programming and now I am stuck, so I would like to ask for your help.
As you can see, the menu is on the left and next to it the "content" section or something like that where i would like to see tables, etc. What are called by the menus when i click on it. How can I call another php code like u can see below? For example, if you click on the customers, it brings out the customers in a table in the "content" section next to the menu without leaving the page and everything is there just there is now the customers and not the settings or something like that.
My index page Code:
<?php
session_start();
// Include login-check file
require_once "login-check.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- The meta viewport will scale my content to any device width -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--Favicon-->
<link rel="icon" type="image/x-icon" href="icons/favicon.png">
<!--Title-->
<title>Learning</title>
<link rel="stylesheet" href="./css/index-sytle.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css">
</head>
<body>
<?php
include "sidebar.php";
?>
<div class="users-table">
</div>
</body>
</html>
My sidebar code:
<div class="wrapper d-flex">
<div class="sidebar">
<ul>
</ul>
<ul>
<li><i class="fas fa-home"></i>Föoldal</li>
</ul>
<small class="text-muted px-3">Ügyfélkezelés</small>
<ul>
<li><i class="fas fa-user"></i>Ügyfelek</li>
<li><i class="fas fa-file"></i>Ajánlatok</li>
<li><i class="fas fa-user"></i>Rendelések</li>
</ul>
<ul>
<li><i class="fas fa-sign-out-alt"></i>Kilépés</li>
</ul>
</div>
And my viewCustomer code which i want to see on the same page "index.php", but near the menus.
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'test');
/* Attempt to connect to MySQL database */
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($conn === false){
die("Nem sikerült csatlakozni..." . mysqli_connect_error());
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if(!$result){
die("Rossz kérés: ") . $conn->error;
}
while($row = $result->fetch_assoc()){
echo $row["id"];
echo $row["username"];
}
?>
Thank You guys for your help!

A simple solution is doing it via the include() and function using data retrieved from the url.
if (isset($_GET["page"])) {
$q = $_GET["page"];
$qa = explode('/', $q);
$page = $qa[0] ?? "home";
//check if a file with the filename retrieved from the $_GET["page"] exists
if (file_exists($pathToFile . $page . '.php')) {
//if it does, include that file
include($pathToFile . $page . '.php');
} else {
//$_GET["page"] is not set, redirect to somewhere it is or.
}
Now you link to you index file only and setting the $_GET["page"] to whichever file you wish to retrieve.
Should look something like this:
index.php?page=filename
the $_GET["page"] here will contain the value "filename"

Related

Why is a file not connecting to the others in PHP forum system?

I'm working on a PHP forum that involves multiple pages leading into each other. I'm at the point where I'm trying to get category topics to appear on a separate page, but for some reason, the topics.php file is connecting to the rest of the files. When I click the home button or try to log out the page doesn't change. I'm doing everything via separate function file.
Here's the topics.php file:
<?php
session_start();
include ('../db.php');
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['message'] = "You must log in before viewing your profile page!";
header("location: error.php");
}
else {
// Makes it easier to read
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
}
include ('content_function.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Home</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom fonts for this template -->
<link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<!-- Custom styles for this template -->
<link href="css/clean-blog.min.css" rel="stylesheet">
<link href="main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
<div class="container">
<a class="navbar-brand" href="../logout.php">Logout</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
Menu <!-- Right above this ^ comment was this code: data-toggle="collapse" #NeverForget-->
<i class="fa fa-bars"></i>
</button>
<div class="nav-link" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-link" class="nav-item">
Home
</li>
<li class="nav-link" class="nav-item">
About
</li>
<li class="nav-link" class="nav-item">
Posts
</li>
<li class="nav-link" class="nav-item">
Contact
</li>
</ul>
</div>
</div>
</nav>
<!-- Page Header -->
<header class="masthead" style="background-image: url('img/bible.jpg')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="site-heading">
<h1>Hi <?= $first_name ?>!</h1>
<span class="subheading">Have Fun Exploring the Fellowship Wall</span>
</div>
</div>
</div>
</div>
</header>
<!-- Main Content -->
<!-- <h1>Categories</h1>-->
<style>
body{
background: tan;}
</style>
<!-- The above code is for the bacckground color on index2.php -->
<hr>
<!-- Pager -->
<!--<div class="clearfix">
<a class="btn btn-primary float-right" href="#">Older Posts →</a>
</div>-->
</div>
</div>
</div>
<?php
/*
$select = mysqli_query($mysqli, "SELECT * FROM categories");
while($row = mysqli_fetch_assoc($select)){
echo "<h1>", $row['category_title']."</h1>" ;
}*/
disptopics($_GET['cid'], $_GET['scid']);
/*if (isset($_GET['cid'], $GET['scid'])){
disptopics();
}*/
?>
<hr>
<!-- Footer -->
<footer>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<ul class="list-inline text-center">
<li class="list-inline-item">
<a href="#">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li class="list-inline-item">
<a href="#">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-facebook fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<!-- <li class="list-inline-item">
<a href="#">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-github fa-stack-1x fa-inverse"></i>
</span>-->
</a>
</li>
</ul>
<p class="copyright text-muted">Copyright © Your Website 2018</p>
</div>
</div>
</div>
</footer>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Custom scripts for this template -->
<script src="js/clean-blog.min.js"></script>
</body>
</html>
Here is the functions file (content_function.php):
<?php
function dispcategories(){
include ('../db.php');
$select = mysqli_query($mysqli, "SELECT * FROM categories");
while($row = mysqli_fetch_assoc($select)){
echo "<table class = 'category-table'";
echo "<tr><td class= 'main-category'colspan='2'>".$row['category_title']."</tr></td>";
dispsubcategories($row['cat_id']);
echo "</table>";
}
}
function dispsubcategories($parent_id){
include ('../db.php');
$select = mysqli_query($mysqli, "SELECT cat_id, subcat_id, subcategory_title, subcategory_descr FROM categories, subcategories
WHERE ($parent_id = categories.cat_id) AND ($parent_id = subcategories.parent_id)");
echo "<tr><th width='90%'>Categories</th><th width='10%'>Topics</th></tr>";
while($row = mysqli_fetch_assoc($select)){
echo "<tr><td class='category_title'><a href='(A)Book 2.0/Bootstrap/topics.php/".$row['cat_id']."/".$row['subcat_id']."'>
".$row['subcategory_title']."<br/>";
echo $row['subcategory_descr']."</a></td>";
echo "<td class='num-topics'>".getnumtopics($parent_id, $row['subcat_id'])."</td></tr>";
}
}
//Displays categories
function getnumtopics($cat_id, $subcat_id){
include ('../db.php');
$select = mysqli_query($mysqli, "SELECT category_id, subcategory_id FROM topics WHERE ".$cat_id." = category_id
AND ".$subcat_id." = subcategory_id");
$get = mysqli_num_rows($select);
return $get;
}
//Displays Topics Within categories
function disptopics($cid, $scid){
include ('../db.php');
$select = mysqli_query($mysqli, "SELECT topic_id, author, title, date_posted, views, replies FROM categories, subcategories, topics
WHERE ($cid = topics.category_id) AND ($scid = topics.subcategory_id) AND ($cid = categories.cat_id)
AND ($scid = subcategories.subcat_id) ORDER BY topic_id DESC");
if(mysqli_num_rows($select) != 0) {
echo "<table class='topic-table'>";
echo "<tr><th>Title</th><th>Posted By</th><th>Date Posted</th><th>Views</th><th>Replies</th></tr>";
while($row = mysqli_fetch_assoc($select)){
echo "<tr><td><a href='readtopic.php/".$cid."/".$scid."/".$row['topic_id']."'>
".$row['title']."</a></td><td>".$row['author']."</td><td>".$row['date-posted'].".</td><td>".$row['views']."</td>
<td>".$row['replies']."</td></tr>";
}
echo "</table>";
} else {
echo "<p>This category has no topics yet! <a href='newtopic.php/".$cid."/".$scid."'> Add a new one!</a></p>";
}
}
?>
I've been looking for days and I can't seem to find a syntax error. Maybe I'm entering something wrong? I'm still pretty new to PHP.
There is a lot of bad going on here. Please don't take that in a mean way, we all have to learn and it's a process. I don't know If I can specifically answer your question but I can point a few things out.
session_start();
include ('../db.php');
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['message'] = "You must log in before viewing your profile page!";
header("location: error.php");
}
else {
// Makes it easier to read
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
}
Nuke the top half of this:
session_start();
include ('../db.php');
// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['message'] = "You must log in before viewing your profile page!";
header("location: error.php");
exit; //<--- add this
}else {
// Makes it easier to read
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
}
It's redundant, because of the If below it. If they are not logged in you redirect the page (these no longer matter), if they are you set these again. Always add exit; after doing the redirect as header doesn't necessarily end the execution of the current script, and it can cause unexpected things to happen.
In your HTML you have
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
Than later in the page you have
<style>
body{background: tan;}
</style>
You should put the style before the Tag, preferably in the HEAD of the page. I am not sure what problems if any this will cause as it likely depends on several things including what browser is being used. It's just that depending on how the page is rendered it may not pick up the style in time to apply it to the tag if the tag is first.
For the functions, I can't say this with certainty because I cant know the full scope of you project but if you only call them once there is no need to make them functions, in fact it complicates things by moving the logic away from where it's used. You can ignore part of that if you do re-use them. But generally, it's best not to echo HTML from a function separate from the HTML, this makes formatting the HTML hard because you have to have both files open. And in the case it's reused, you may be modifying a page it's used on without knowing that ahead of time. It's better to return the data then do the formatting inline. This too is subjective because it really depends on how it's done in the grand scheme of the project. But a word of caution. Some of this is based on the procedural nature of the code, but even in Object orientated programming (OOP) where classes are used, it's still not a good idea to output HTML directly from a class for the same reason.
Connection.
function getnumtopics($cat_id, $subcat_id){
include ('../db.php');
Your including this multiple times, besides wasting processing power and time, you also create a hard dependency on that file. If you choose later to replace that file you have a lot of code to edit. It's better to include/require that once like you do at the top of the page and then pass the connection into the functions when calling them. So instead of this:
function getnumtopics($cat_id, $subcat_id){
include ('../db.php');
$select = mysqli_query($mysqli, ...
Do something like this
function getnumtopics($cat_id, $subcat_id, $mysqli){
$select = mysqli_query($mysqli, ...
And when you call it you just add that to the call, provided it's included/require in the file that called it. If you are not sure and you need it in multiple files then you can use 'require_once' (more on this later). An example of the problems this causes. Say sometime later you want to create a new DB file (for whatever reason, I cant think of one, but it could happen). With that filename backed into all the functions you can't test it just on one page without modifying all the functions used in the page, some of which may be used on other pages. So you are stuck going though all that code searching and replacing the filename. That is probably a bad example but it's the best I can think of right now. It's just a bad practice for several reasons.
Require Vs Require Once
File 1.
require_once('../db.php');
require_once('../file2.php');
....
getnumtopics($cat_id, $subcat_id, $mysqli);
File 2.
require_once('../db.php');
....
dispsubcategories($cat_id, $subcat_id, $mysqli);
In this example File1 needs the DB, but also File2 does. But maybe you use File2 also in a File3 that doesn't need it:
File 3.
require_once('../file2.php');
In this case if you just require it, and you should require not include it. Then you get 2 copies in the File1 case and 1 copy in the File3 case. You cannot just include it in File2 because File1 needs it as well. So you can use the _once this insures that if its all ready been included you won't include it again, but also if it hasn't that it will be (this made more sense in my head).
Include Vs Requre
As for include vs require. Include will not throw any errors if the file is missing, require will. So you have to ask yourself will this script function properly if file x (file to be included) is not found. In this case that answer is no. So you should make it required. This will help you debug errors in the event that the file goes missing. You could move it to Linux where casing (capital vs lowercase) matters, and it worked fine on Windows but now it's busted. This will help you by telling you the required file is missing, instead of getting a general error for failing to connect to the DB or for calling the DB functions on a NULL resource.
Summery
As to why it's combining the pages, I am not sure it could be due to not calling exit, but I can't say that for sure as I haven't studied the behavior of not calling exit in any great detail I only know that you should always call it after a redirect or weird things can happen.
I also can't say this is everything that is wrong, it's just what I see right away that I would change.
Hope that helps.

php giving out blank pages (may be website setup)

Hi all I don't know if anyone will or can help me but for the last week or so I have been trying to make up a profile page setup on my website but I can not get the pages to register any echo's or jest even php (It gives me blank pages and or tell me an error 500 server error)
I have set up my website with a static page for my logo, navbar and footer and using a include line to get the content on the pages. (I hope that makes sense).
if I put any form of php into the content pages it give me errors of 500 server error and or the pages just turns completely white.
Is there a specific way to show SQL content and or any PHP with this set up? if anyone knows any tutorials or could lend a hand that would be amazing (Any help will be placed onto the website on the Credits page).
Here is the static page code:
<?php require '../connection/conn.php'; ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="../css/Master.css" />
<link rel="stylesheet" type="text/css" href="../css/Menu.css" />
<link rel="stylesheet" type="text/css" href="../css/AboutImages.css" />
<link rel="stylesheet" type="text/css" href="../css/Form.css" />
</head>
<body>
<div class="container">
<div class="header">
<div id="logindata"></div>
</div>
<div class="menu">
<nav>
<ul class="cssmenu">
<li>Home</li>
<li>About Us</li>
<li>Videos</li>
<li>Contact Us</li>
<li>Forum</li>
<li></li>
<li>My Account</li>
<li>Logout</li>
</ul>
</nav>
</div>
<div class="content">
<?php echo $content;?>
</div>
<div class="footer">
<p>All Rights Reserved, LPGamers.com, Created And Built by Robert Prince & Amber Milton-White</p> Credits Page
</div>
</div>
</body>
</html>
And the content page:
<?php
$title = 'LPGamers -- Personal Account';
$content = '
<div id="formbox">
<form action="" method="GET">
<p>Search for you friends here !</p>
<input type="text" class="tfield">
<input type="submit" class="button">
</form>
</div>
<div class="ppicture">
<img src="" />
</div>
<div class="pd">
</br>
<p class="pdp">
Account id: # $userid
</p>
</br>
<p class="pdp">
Name:
</p>
</br>
<p class="pdp">
Age:
</p>
</br>
<p class="pdp">
Your Email:
</p>
</br>
<p class="pdp">
Your Bio:
</p>
</div>
';
include ("Site_View.php");
?>
If this help any more Thanks in advance
Edited the answer, it looks like some content of the original post was not displayed initially.
Do not put the form in the PHP code, it is not needed. You want to put the result probably there.
If you want to get data from SQL in PHP do the following:
Prerequisite: setup the php.ini to use whatever database you need, like:
extension=php_sqlsrv_56_nts.dll
Define a database connection
$serverName = "myserver";
$usr="mySQLuser";
$pwd="mySQLpass";
$db="mydatabase";
$connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" => $db);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( !$conn ) {
die( print_r( sqlsrv_errors(), true));
}
Use the SQL connection to retrieve data.
$sql = "Select name from Users where users.name = " . $myparam;
note: the example above is vulnerable to SQL injection, but it is an easy start for coding, you can fix this later when you are familiar with it. Also you need to give a value to $myparam first.
Then:
$stmt = sqlsrv_query($conn, $sql);
sqlsrv_next_result($stmt);
sqlsrv_fetch($stmt);
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
Now you can use the data when you want, like this:
<?php
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$returnedName = $row['name'];
echo $returnedName;
}
?>

PHP Script is connecting to database but will not return a table value despite being connected.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.0.2/material.indigo-pink.min.css">
<script src="https://storage.googleapis.com/code.getmdl.io/1.0.2/material.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<body>
<!-- Always shows a header, even in smaller screens. -->
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<!-- Title -->
<span class="mdl-layout-title">Forum</span>
<!-- Add spacer, to align navigation to the right -->
<div class="mdl-layout-spacer"></div>
<!-- Navigation. We hide it in small screens. -->
<nav class="mdl-navigation mdl-layout--large-screen-only">
<a class="mdl-navigation__link" href="">Default Subforum2</a>
</nav>
</div>
</header>
<div class="mdl-layout__drawer">
<span class="mdl-layout-title">Forum</span>
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="">Browse Subforums</a>
<a class="mdl-navigation__link" href="">Search For Post</a>
</nav>
</div>
<main class="mdl-layout__content">
<div class="page-content">
<!-- Your content goes here -->
<?php
include("dbforforum.php");
$subForum = "";
$results = $mysqli->query("SELECT * FROM `SubForumList`");
echo $results;
for ($i = 0; $i < $results->num_rows; $i++)
{
$results->data_seek($i);
$row = $results->fetch_assoc();
$subForum = $row['Name'];
echo $subForum;
echo $subForum;
}
?>
</div>
</main>
</div>
</body>
</html>
I'm trying to echo HTML within PHP the row 'subForum' of my SQL table $row['subForum'] every time this loop goes through, however, for some reason it is not connecting.
Even in dbforforum.php, my database config file, it is showing that it connected successfully:
<?php
$mysqli = new mysqli("localhost", "root", "", "Forum");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I am very, very confused by this. Any help is appreciated.
Note: I recently switched to Mac from Windows (not my choice), and have therefore gone from WAMP to MAMP. Not sure if this has to do with anything.
It should be this
In your database config
$mysqli = new mysqli("localhost", "root", "", "Forum"); //the variable here is mysqli
if ($conn->connect_error) { //the error here is $conn because it isn't the variable above so change the variable to $mysqli
die("Connection failed: " . $conn->connect_error); //and here too to $mysqli
}
echo "Connected successfully";
Changes:
$mysqli = new mysqli('localhost', 'root', '', 'databasename'); //connect db
if($mysqli->connect_errno){
die("Error! ".$mysqli->connect_errno);
}
if($result = $mysqli->query("SELECT * FROM SubForumlist")){
echo $result->num_rows //num_rows == your data you want to echo
}

Retrieve Single User Data Using Session After He Logged In [duplicate]

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...

Nav Bar issue: only show links in nav bar when logged in as admin

I want my code only show this when im logged in as an admin
I have written this code for it exept when i add something like
in the php code it crashes
so when i use:
</html>
//check if logged in
<?php
session_start();
$loggedin = $_SESSION['loggedin'];
$loggedinadmin = $_SESSION['positief'];
?>
//or do i have to use the code below to check if logged in (i can leave out the if ($loggedin)
<?php
session_start(); // Start the session
$loggedin = $_SESSION['loggedin']; // Are they loggedin?
// They are not logged in, Kill the page and ask them to login.
if ($loggedin != "1") {
die('Sorry you are not logged in, please click Here to login');}
$logadmin = $_SESSION['logadmin']; // Are they loggedin?
// They are not an admin, Kill the page and ask them to login.
if ($logadmin != "1") {
die('You have no POWER here! click Here return to your shell');}
//in the line above does it need an edit? so it doesn't show the items you have no rights for?
?>
// css code
<head>
<link href="style2.css" type="text/css" rel="stylesheet"/>
</head>
//nav bar and links
<div id="nav">
<div id="nav_wrapper">
<ul>
<li>Home</li>
<li>Login</li>
<li>Edit App</li>
<li>Preview</li>
<li>Logout</li>
//EXEPTION for admin
<?php
$adminit="IT Admin";
$verzend="Verzendingen";
$verzond="Verzonden";
$magazij="Magazijn";
if ($loggedinadmin) {
echo "<li>$adminit</li>";
echo "<li>$verzend</li>";
echo "<li>$verzond</li>";
echo "<li>$magazij</li>";
}
?>
</ul>
</div>
</div>
</html>
This is my code, i made some notices to explain what i have done.
or ask for advice.
Anyone taking time to look at this and try to help thanks in advance!
Greetings,
DTcodedude
You can add your php code inside your html code like this.
<li><?php echo $adminIT; ?></li>
Solution by OP.
<html>
<?php
session_start();
$loggedin = $_SESSION['loggedin'];
$logadmin = $_SESSION['logadmin'];// checks if you got adminrights
// (if intrested in auth code post below).
$home="index.php";
$login="login.php";
$edit="navbase.php";
$preview="preview.php";
$logout="logout.php";
$adminit="adminbase.php";
$verzend="verzend.php";
$verzond="verzonden.php";
$magazij="magbase.php";
$host="";
$username="";
$password="";
$db_name="";
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect Host");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM users";
$result = mysql_query($sql, $conn);
?>
<head>
<link href="style2.css" type="text/css" rel="stylesheet"/>
</head>
<div id="nav">
<div id="nav_wrapper">
<ul>
<?
if ($result) {
echo " <li>Home</li>";
echo " <li>Login</li>";
echo " <li>Edit App</li>";
echo " <li>Preview</li>";
echo " <li>Logout</li>";
} else {
die ('crash');
}
?>
<?
if ($logadmin) {
echo " <li>IT Admin</li>";
echo " <li>Verzendingen</li>";
echo " <li>Verzonden</li>";
echo " <li>Magazijn</li>";
}else{
}
?>
</ul>
</div>
</div>
</html>

Categories