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>
Related
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);
So I am using XAMPP but when I click on a link it redirects me to localhost/dashboard instead of the page I want. How can I fix this?
This is the code of the index page
<!DOCTYPE html>
<html>
<head>
<title>SG Weredi</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="loginpagina.js"></script>
</head>
<body>
<div class="header">
Your App Name
</div>
<div class="container-fluid">
<img class =" logo" src="http://www.weredi.nl/logo.png">
</div>
<?php if( !empty($user) ): ?>
<br />Welcome <?= $user['email']; ?>
<br /><br />You are successfully logged in!
<br /><br />
Logout?
<?php else: ?>
<h1>Please Login or Register</h1>
Login or
Register
<?php endif; ?>
</body>
</html>
The only link that is correct is Register.
You should do the same for login:
Login
instead of
Login.
The same solution is for link to home page of project:
<div class="header">
Your App Name
</div>
instead of
<div class="header">
Your App Name
</div>
because href="/" on your site redirects you to index.php in C:/xampp/htdocs which redirects to C:/xampp/htdocs/dashboard/ (localhost/dashboard).
Try Changing port number to new one and restart the apache server.
I changed port number from 8082 to 8084 and restarted the server. This worked for me.
I have two PHP pages:
giardino.php:
<?php
SESSION_START();
if (isset($_SESSION['utente'])){
?>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" type="image/x-icon" href="immagini/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
<link rel="stylesheet" href="css/stile.css"/>
</head>
<body>
<div data-role="page" data-theme="d">
<div data-role="header">
Ambienti
<a href="logout.php" data-role="button" data-theme="b" data-icon="delete" >Logout</a>
<h1>Domos - Giardino</h1>
</div><!-- /header -->
<div data-role="content" id="centramento">
<?php
include("configurazione.php");
$id_stanza=1;
$pin=26;
//lettura stato attuale
$comando="select luci from casa where ID_stanza=1";
$query=mysql_query($comando,$connessione);
while($riga=mysql_fetch_array($query)){
$oldstate=$riga['luci'];
}
if($oldstate == 0){
$newstate='accendi luce';
$theme='e';
}
else{
$newstate='spegni luce';
$theme='a';
}
echo "<a href='luce.php' data-role='button' data-theme='$theme' id='radio'>$newstate</a>"
?>
</div><!-- /content -->
<div data-role="footer" data-position="fixed">
<h4>Credits: Silvio Mattiello 5C Informatica 2014/2015</h4>
</div><!-- /footer -->
</div><!-- /page -->
</body>
</html>
<?php
}
else{
header("location:home.php?msg=6");
}
?>
luce.php:
<?php
$id_stanza=1;
$pin=$_GET=26;
$comando = escapeshellcmd("sudo /var/www/domotica/python/luce.py $pin $id_stanza");
$esito = exec($comando);
if ($esito == "allarme attivato"){
header("location:ambienti.php");
exit();
}
else {
header("location:giardino.php");
exit();
}
?>
When I click the button on the first page, I get redirected to the second page.
In the second page, there are some operations and then I get redirected to the first page (or another page, ambienti.php).
But when I am redirected to the first page again, the button on this page (giardino.php) doesn't work. I can't click it. Why?
try to use a require instead of the include, to see if there is any error... It seems to be something with include.
Also, you could try to add 302 http to the header like this and add the full path
header('Location: http://server_name/ambienti.php', true, 302);
So I am trying to access a certain webpage within my site.
In my index, I use a isset($_GET() parameter to access another page. My code for the index.php is as follows:
<!DOCTYPE html>
<html>
<head>
<?php require ("includes/database.php"); ?>
<title>Offstreams Admin Panel</title>
<link rel="stylesheet" type="text/css" href="styles/admin_body.css" />
<link rel="stylesheet" type="text/css" href="styles/admin_header.css" />
<link rel="stylesheet" type="text/css" href="styles/admin_footer.css" />
<link rel="stylesheet" type="text/css" href="styles/admin_postspace.css" />
</head>
<body>
<header>
<!--Header Info Here -->
</header>
<div class="wrapper">
<div class="sidebar">
<ul>
<li>Post New Band</li>
</ul>
</div>
<article>
<?php
if (isset($_GET['post_new_band'])){
require ("includes/post_new_band.php");
echo "Page accessed";
} else {
echo "Page not accessible.";
}
?>
</article>
</div>
</body>
</html>
I have it echo page not available when "index.php?page" doesn't exist. But when it does exist, such as "index.php?post_new_band" (code below) nothing is posted. The sever and database work, that is for sure. MySQL isn't the problem since I am trying to get the html to work.
Code for "post_new_band.php":
<!DOCTYPE html>
<html>
<head>
<!-- Head Info Goes Here -->
</head>
<body>
<h1>Insert New Band</h1>
<form action='index.php?post_new_band' method='post'>
<b>Insert New Band</b><input type='text' name='band_name' />
<b>Insert Band Origin</b><input type='text' name='band_origin' />
<input type='submit' name='insert_band' value='Add Band' />
</form>
<?php
if (isset($_POST['post_new_band'])){
$band_name = $_POST['band_name'];
if($band_name==''){
echo "<script>alert ('Please Insert Band Name')</script>";
echo "<script>window.open('index.php?post_new_band','_self')</script>"
} else {
$insert_band_name = "insert into Band (band_name) values ('$band_name')";
$run_band_name = mysql_query("$insert_band_name");
echo "<script>alert ('New Category Added')</script>";
echo "<script>window.open('index.php?post_new_band','_self')</script>"
}
}
?>
</body>
</html>
You can only use a request key once - so dont use $_GET['post_new_band'] and $_POST['post_new_band'] in the same request. Change one of the keys.
This is pretty stupid, but it actually turned out to be a syntax error involving semi-colons. *Slap to the face.
i used sessions to log a user in so basically the user is stored in a session. what code do i use to stick into my following file so a user cannot access the page unless he/she is logged in.
<?php
session_start();
?>
<!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>Cook It Dot Com</title>
<link href="../style.css" rel="stylesheet" type="text/css" />
</head>
<body><div id="wrap">
<div id="header"></div>
<div id="nav">
<ul id='menu'>
<li><a href="../usersloggedin/starters.php" >Starters</a></li>
<li><a href="../usersloggedin/mains.php" >Mains</a></li>
<li><a href="../usersloggedin/vegeterian.php" >Vegeterian</a></li>
<li>Desserts</li>
</ul>
<ul id="rightmenu">
<li> Logout</li>
</ul>
</div>
<div id="content">
<div id="info" align="justify"><FONT COLOR="white">
My Account -
<?php
session_start();
require_once '../database.php';
if (isset($_SESSION['myusername'])){
echo "Welcome ".$_SESSION['myusername'];
}
?>
<hr />
<br /> Upload Your Recipes<br /><br />
Upload Starter
Upload Mains
Upload Vegeterian
Upload Desserts
<br /><br />Edit/Delete Your Recipes<br/><br/>
Starter
Mains
Vegeterian
Desserts
</div></div>
<div id="footer"><div id="footerinfo" align="center">Copyright Cook It Dot Com 2011 - Designed By Jahedul Hussain - </a></div></div>
</div>
</body>
</html>
Thanks A LOT!
How about something like
if(!isset($_SESSION["user"]))
{
header("Location: homepage.php");
}
Basically, if there is not a user session redirect to the homepage.
The exit() or die() functions, or, if you want to show certain content, a simple if statement containing HTML. E.g.
<?php if($logged_in) { ?>
<p>You are logged in!</p>
<?php } else { ?>
<p>You're not logged in. Go to the login page.</p>
<?php } ?>