Change navigation bar if user is logged in - php

I want the "Login/Sign up" button in the navigation bar to change to "My Account" after the user logs in. I have a problem as my index.html page does not display the navigation bar.
index.html page:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Index Page</title>
</head>
<body>
<?php include("navigation.php"); ?>
</body>
</html>
navigation.php
<?php
include("check.php");
?>
<?php
if ($loginst == 1){ ?>
<div id="nav">
<ul >
<li class="navbar-left">Page 2</li>
<li class="navbar-left">Page 3</li>
<li class="navbar-right">My Account</li>
<li class="navbar-right"><a href="logout.php">Sign Out</a</li>
</ul>
</div>
<?php } else { ?>
<div id="nav">
<ul >
<li class="navbar-left">Page 2</li>
<li class="navbar-left">Page 3</li>
<li class="navbar-right"><a href="login.php">Login</a</li>
</ul>
</div>
<?php } ?>
check.php
<?php
include('connection.php');
session_start();
$loginst = 0;
if ($_SESSION['username']){
$user_check = $_SESSION['username'];
$ses_sql = mysqli_query($db,"SELECT username FROM users WHERE username='$user_check' ");
$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_user=$row['username'];
if(!empty($login_user))
{
$loginst = 1;
}
}
?>
If I go to index.html then nav bar is not displayed. What am I doing wrong? Thanks.

Because your file format is html and does not support php tags :
<?php include("navigation.php"); ?>
rename index.html to index.php for support php include.

Related

Rendering another layout inside index.php

I am following this Link to understand the basic framework of PHP Web app but I am not able to understand how can I switch back and forth to the different layouts (Articles, Portfolio) through navigation bar?
All different layout must be like index.php.
Image you have articles.php and portfolio.php. There content would be like:
Articles.php:
<?php
// load up your config file
require_once("/path/to/resources/config.php");
require_once(TEMPLATES_PATH . "/header.php");
?>
<div id="container">
<div id="content">
<!-- Your article content here!!! -->
</div>
<?php
require_once(TEMPLATES_PATH . "/rightPanel.php");
?>
</div>
<?php
require_once(TEMPLATES_PATH . "/footer.php");
?>
and portfolio.php:
<?php
// load up your config file
require_once("/path/to/resources/config.php");
require_once(TEMPLATES_PATH . "/header.php");
?>
<div id="container">
<div id="content">
<!-- Your portfolio content here!!! -->
</div>
<?php
require_once(TEMPLATES_PATH . "/rightPanel.php");
?>
</div>
<?php
require_once(TEMPLATES_PATH . "/footer.php");
?>
But everytime you add a new page, you must update your header.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple Site</title>
</head>
<body>
<div id="header">
<h1>Simple Site</h1>
<ul class="nav global">
<li>Home</li>
<li>Articles</li>
<li>Portfolio</li>
<li>Any other page</li>
</ul>
</div>
This is what actually I wanted to achieve
<nav>
<ul>
<li>Home</li>
<li>Projects</li>
<li>Calculator</li>
<li>Blog</li>
<li>About</li>
</ul>
</nav>
I am sending the page variable to the index and then it is rendering the layout accordingly through this way.
if (isset($_GET["page"])){
$page=$_GET["page"];
}
else {
$page = "home";
}
renderLayoutWithContentFile($page .".php", $variables);

Redirecting HomePage

Iam Learning PHP, so i have started an website..in that i have created index.php and index_1.php and login.php in which when user successful login the user has to be redirect to index.php but iam not getting it so i have created an another index_1.php in this page only three navigation bars will be there Home,projects and contactus.
This is my index.php Page
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<li>Login</li>
<li>Register</li>
<li>Contact</li>
</ul>
</div>
</div>
This is my index_1.php
<?
session_start();
if(!isset($_SESSION['user_email']))
{
echo '<p>Please Login to continue Log In</p>';
exit();
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<li>Contact</li>
<?php
echo '<p align="right">';
session_start();
echo "Welcome";
echo '<br>';
echo ($_SESSION ['user_email']);
echo '<br> Logout?ClickHere</p>';
?>
This is my login.php
<?php
ob_start();
session_start();
if(isset($_POST["Submit"]))
{
$user_email=$_POST['user_email'];
$user_password=md5($_POST['user_password']);
$con=#mysql_connect('localhost','xxxxx','xxxx') or die(mysql_error());
mysql_select_db('suryapra_aenugula_karthik');
$query=mysql_query("SELECT * FROM user_registration where user_email='".$user_email."' AND user_password='".$user_password."'") or die("error in selection");
$numrows=mysql_num_rows($query);
if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
$dbusername=$row['user_email'];
$dbpassword=$row['user_password'];
}
if($user_email==$dbusername && $user_password==$dbpassword)
{
if(isset($_POST['remember']))
{
setcookie('user_email',$user_email,time()+60*60*7);
setcookie('user_password',$user_email,time()+60*60*7);
}
session_start();
$_SESSION['user_email']=$user_email;
header("Location: index_1.php");
ob_end_flush();
}
}
else
{
header("Location: login_2.php");
ob_end_flush();
}
}
else
{
header("Location: login.php");
}
?>
what my problem is if user logins he is redirecting to index.php and again login and register links are also getting in index.php
i want solution in which when user logins he should be redirect to index.php and login,register tabs should not be visible to him unless he presses logout button
The basic flow of User Authentication:
User submits login form to login.php this, if login is correct, will set a $_SESSION variable to signal user is logged in.
User is then redirected back to site, where
In the templates, anything that is optional for loggedin/logged out users are wrapped in IF blocks
Something like so:
<?php
$logged_in = $_SESSION['logged_in'];
?>
<nav>
<?php if (!$logged_in):?>
Login
<?php endif;?>
<?php if ($logged_in):?>
Logout
<?php endif;?>
</nav>
First of all after a successful login on the login page redirect user on the index.php page.
you dont need index_2.php page.
After that edit your index page with following code where first we will check if [user_email] session is been created or not when we logged in if it is created we are not echoing anything if not then we will echo the login and register link. bellow is your new index.php page
<?php session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<?php if(isset($_SESSION[user_email]))
{
}
else
{
echo "<li><a href='login.php'>Login</a></li>";
echo "<li><a href='register.php'>Register</a></li>";
}
?>
<li>Contact</li>
</ul>
</div>
</div>

Filter in php/mysql with PDO

I did one webpage with pagination in php. But i want to include some filter to search in this pagination. So if someone has good advice, will be good received.
Here is the file where i did the pagination. The name is, Index.php:
<?php
require_once 'database.php';
$database_connection = database_connect();
$title='hola';
$content='';
//user input
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 2;
//Positioning
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$art = $database_connection->query("SELECT id FROM coffee");
//Query
$articles = $database_connection->prepare("SELECT id FROM coffee LIMIT $start,$perPage");
$articles->execute();
$articles = $articles->fetchAll();
$resultado = $database_connection->query("SELECT COUNT(*) AS total FROM coffee");
$fila = $resultado->fetch(PDO::FETCH_ASSOC);
$total = $fila["total"];
$pages = ceil($total/$perPage);
include 'Template_1.php';
?>
And here is my html website. Template:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="Styles/Stylesheet.css" />
</head>
<body>
<div id="wrapper">
<div id="banner">
</div>
<nav id="navigation">
<ul id="nav">
<li>Home</li>
<li>Coffee</li>
<li>Shop</li>
<li>About</li>
</ul>
</nav>
<div id="content_area">
<?php
require_once("tool.php");
foreach ($articles as $article):
echo $article['id'];?>
</br><?php
endforeach;?></br><?php
for($x=1;$x<=$pages;$x++):?>
<?php echo $x;?>
<?php endfor;?>
</br>
<?php echo $content; ?>
</div>
<div id="sidebar">
</div>
<footer>
<p>All rights reserved</p>
</footer>
</div>
</body>
</html>
I'm not sure if i know what you mean by "But i can't combine search with my pagination." But as i understand you want to add some kind of a 'where' statement in your query.
Here are examples of doing it and i think the last code from this site is what you want: http://www.webreference.com/programming/php/search/2.html

Redirect button in php [duplicate]

This question already has answers here:
What does "href" stand for? [duplicate]
(5 answers)
Closed 7 years ago.
Hello guys I want to redirect a user to another page when clicking on a button on my webpage, but I dont know how to do it. My buttons are "home", "coffee", "shop" and "about us". When clicking on any of these, I want to get to another page. Can anyone help please?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="Styles/stylesheet.css" />
</head>
<body>
<div id="wrapper">
<div id="banner">
</div>
<nav id="navigation">
<ul id="nav">
<li>Home</li>
<li>Coffee</li>
<li>Shop</li>
<li>About us</li>
</ul>
</nav>
<div id="content_area">
<?php echo $content; ?>
</div>
<div id="sidebar">
</div>
<footer>
<p>All rights reserved</p>
</footer>
</div>
</body>
</html>
and this is my index code:
<?php
$title = "Coffee Webpage";
$content = '<img src="https://encrypted-tbn2.gstatic.com/images? q=tbn:ANd9GcSmRPsVdzF4BYRWbF3yBpbkW-1ZjyURYWX1JQm0e6RAbJPR0H76" class="imgLeft" />
<h3>Title 1</h3>
<p>
</p>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTdJv4yTpue170rTnOn9unKMgYWeOSPxBP9LoBwvUvhcDJNFcPG6w" class="imgRight"/>
<h3>Title 2</h3>
<p>
</p>
<img src="http://seattle.eat24hours.com/files/cuisines/v4/coffee.jpg?e24v=225" class="imgLeft" />
<h3>Title 3</h3>
<p>
</p>';
include 'Template.php';
?>
add the url to that page in to your link so for example if the page coffes is coffe.php then put coffe.php in there.
alternately put your full url which I would recommend.
you could also add this before your link to get the site url using php
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
Typically, you'd want to do this by adding a front controller; or, in your case, turning your index.php into a front controller.
<?php
$page = $_GET['page'];
switch($page) {
case 'coffee':
// set variables
break;
case 'another':
// set variables to something else
break;
}
require 'template.php';
Put a link to each page in their corrosponding a href attributes.
Example:
<li>Coffee</li>
<li>Shop</li>
<li>About us</li>

MySQL and PHP display error

Hello i m new to MySQL and PHP under training please tell me where i m wrong
My problem occurs in ad01 and ad03 they are echo Advertise Here but ad02 echo properly
<?php require_once('Connections/localhost.php'); ?>
<?php
mysql_select_db($database_localhost, $localhost);
$query_advtDisplay = "SELECT * FROM advt";
$advtDisplay = mysql_query($query_advtDisplay, $localhost) or die(mysql_error());
$row_advtDisplay = mysql_fetch_assoc($advtDisplay);
$totalRows_advtDisplay = mysql_num_rows($advtDisplay);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download Links</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="wrapper">
<div class="ad01">
<?php if ($row_advtDisplay['advt-no']=='ad01')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?>
</div>
<div class="middlebox">
<div class="ad02">
<?php if ($row_advtDisplay['advt-no']=='ad02')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?>
</div>
<div class="linkbox">
<p>Download Links</p>
<ul>
<li>Link 01</li>
<li>Link 02</li>
<li>Link 03</li>
<li>Link 04</li>
<li>Link 05</li>
</ul>
<ul>
<li>Link 06</li>
<li>Link 07</li>
<li>Link 08</li>
<li>Link 09</li>
<li>Link 10</li>
</ul>
<ul>
<li>Link 11</li>
<li>Link 12</li>
<li>Link 13</li>
<li>Link 14</li>
<li>Link 15</li>
</ul>
</div>
<div class="passwordbox">
<p>RAR Password</p>
</div>
</div>
<div class="ad03"><?php if ($row_advtDisplay['advt-no']=='ad03')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?></div>
<div class="clear"></div>
</div>
</body>
</html>
<?php
mysql_free_result($advtDisplay);
?>
only ad02 shows properly not ad01 and ad03 No Problem with Table only Coding Problem sorry for bad english
mysql_fetch_assoc will only return one row ever... so you need to loop through to see all your data.
$just_one_row = mysql_fetch_assoc($advtDisplay);
// how to loop through all the rows
while ($row = mysql_fetch_assoc($advtDisplay)) {
echo $row["advt-no"];
echo $row["advt-content"];
}
// maybe do something like...
$mydata = array();
while ($row = mysql_fetch_assoc($advtDisplay)) {
$mydata[ $row['advt-no'] ] = $row['advt-content'];
}
<!-- and then -->
<div class="ad01">
<?php
if ( isset($mydata['ad01']) && !empty($mydata['ad01']) ) {
echo $mydata['ad01'];
} else { echo "Advertise Here"; } ?>
</div>
and I have to say it... you should really be using PDO or mysqli.

Categories