How to restrict users from entering a page without being logged on - php

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 } ?>

Related

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>

Display icon on profile if user has been defined as $admin

I am working on a little forum, and I want it to be easy to identify a staff member.
A the moment, a user is defined as an $admin in the config file:
<?php
/******************************************************
------------------Required Configuration---------------
Please edit the following variables so the forum can
work correctly.
******************************************************/
//We log to the DataBase
mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('forum_database');
//Forum Staff
$admin='The_Darthonian'; // For admin forum features
/******************************************************
-----------------Optional Configuration----------------
******************************************************/
//Forum Home Page
$url_home = 'index.php';
//Design Name
$design = 'default';
/******************************************************
----------------------Initialization-------------------
******************************************************/
include('init.php');
?>
I have an icon at the path of default/images/role_moderator.gif that I want to appear on a profile if a user is defined as an admin
I further have a userid variable. For example, the first account would be 1, then the second 2 and so on which are unique. Below is the profile code:
<?php
//This page display the profile of an user
include('config.php');
?>
<!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" />
<link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
<title>User Profile</title>
</head>
<body>
<div class="header">
<img src="<?php echo $design; ?>/images/logo.png" alt="Forum" />
</div>
<div class="content">
<?php
if(isset($_SESSION['username']))
{
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['userid'].'" and user1read="no") or (user2="'.$_SESSION['userid'].'" and user2read="no")) and id2="1"'));
$nb_new_pm = $nb_new_pm['nb_new_pm'];
?>
<div class="box">
<div class="box_left">
Home > Users > Profile
</div>
<div class="box_right">
Your messages(<?php echo $nb_new_pm; ?>) - <?php echo htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8'); ?> (Logout)
</div>
<div class="clean"></div>
</div>
<?php
}
else
{
?>
<div class="box">
<div class="box_left">
Home > Users > Profile
</div>
<div class="box_right">
Sign Up - Login
</div>
<div class="clean"></div>
</div>
<?php
}
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
$dn = mysql_query('select username, email, avatar, signup_date from users where id="'.$id.'"');
if(mysql_num_rows($dn)>0)
{
$dnn = mysql_fetch_array($dn);
?>
This is the profile of "<?php echo htmlentities($dnn['username']); ?>" :
<?php
if($_SESSION['userid']==$id)
{
?>
<br /><div class="center">Edit my profile</div>
<?php
}
?>
<table style="width:500px;">
<tr>
<td><?php
if($dnn['avatar']!='')
{
echo '<img src="'.htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8').'" alt="Avatar" style="max-width:100px;max-height:100px;" />';
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<?php
?>
<div>
<img src="default/images/no_avatar.jpg" alt="no_avatar" />
</div>
</body>
</html>
<?php
}
?></td>
<td class="left"><h1><?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?></h1>
This user joined the website on <?php echo date('Y/m/d',$dnn['signup_date']); ?></td>
</tr>
</table>
<?php
if(isset($_SESSION['username']) and $_SESSION['username']!=$dnn['username'])
{
?>
<br />Message "<?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?>"
<?php
}
}
else
{
echo 'We could not find this user anywhere. Prehaps their account was removed.';
}
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<?php
?>
<div>
<img src="/default/images/no_avatar.jpg" alt="no_avatar" />
</div>
</body>
</html>
<?php
}
?>
</div>
<div class="foot">About Us - Terms of Service</div>
</body>
</html>
How can I make it so that if a user is defined as an $admin in the config, they have the icon appear on their profile?

How to re-add form data after preview and going back?

I have a webpage and a place to submit data. I made a preview page for this, and whenever a user clicks 'preview' he can see what it would look like. The trouble is whenever they click the back button from the preview all the data is gone. How do I avoid this and keep the data without any very complex solutions?
preview.php
<?php
session_start();
$getTitle = $_POST['title'];
$getEntry = $_POST['entry'];
date_default_timezone_set('UTC');
$getTime = date('D, M jS, o, H:i a e');
$user = $_SESSION['username'];
?>
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<link rel="stylesheet" type="text/css" href="css/main.css">
<head> <title> Blog - Preview </title> </head>
<body>
<div class="wrap">
<div class="navPreview">
<ul>
<li>Back<br></li>
</ul>
</div>
<div class="main">
<h1>Preview</h1>
<div class="mainscroll">
<?php
echo "<span>Submitted at: $getTime by $user</span><br>";
echo "<h2>$getTitle</h2>";
echo "<p>$getEntry</p><hr>";
?>
</div>
</div>
</div>
<div class="footer">x</div>
</body>
addentry.html
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<link rel="stylesheet" type="text/css" href="css/main.css">
<head> <title> Blog - Add Entry </title> </head>
<body>
<div class="wrap">
<div class="header">
<h2> Welcome.. </h2>
<p>..add an entry below</p>
</div>
<div class="nav">
<ul>
<li>Home<br></li>
<li>Add Entry<br></li>
<li>Logout<br></li>
</ul>
</div>
<div class="main">
<form id="entryForm" action="addentry.php" method="post">
<p class="title">
<label>Title:</label>
<input type="text" name="title"><br>
</p>
<p class="body">
<label>Entry:</label>
<textarea name="entry"></textarea><br>
</p>
<p class = "buttons">
<script type="text/javascript">
function clearConfirm() {
var confirm = window.confirm("Are you sure you want to clear?");
if (confirm) {
document.getElementById("entryForm").reset();
}
}
function previewForm(action) { document.getElementById('entryForm').action = action;
document.getElementById('entryForm').submit();
}
</script>
<input type="button" onclick="clearConfirm()" value="Clear" />
<input type="button" onclick="previewForm('preview.php')" value="Preview" />
<input type="submit" value="Submit" />
</p>
</form>
</div>
<div class="footer">x</div>
</div>
Maybe add a target="_blank" ? It will open the preview in a new tab.
<input type="button" onclick="previewForm('preview.php')" value="Preview" target="_blank"/>
EDIT: Oups not in the form but on your preview link...
Or something like this : http://www.w3schools.com/tags/att_button_formtarget.asp

Page reapets it self when using include

So Lately i was working on a website on LocalHost Using XAMPP Application.
So i created header.php with the code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="A short description." />
<meta name="keywords" content="put, keywords, here" />
<title>Website Name</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div id="wrapper">
<div id="menu">
<a class="item" href="index.php">Home</a>
<a class="item" href="../forum/">Forums</a>
<a class="item" href="live-chat.php">Live Chat</a>
<a class="item" href="Login.php">Log In</a>
<a class="item" href="Register.php">Register Now!</a>
<div id="userbar">
<?php
error_reporting(E_ALL & ~E_NOTICE);
include 'search.php';
if($_SESSION['signed_in'])
{
echo 'Welcome <b>' . htmlentities($_SESSION['user_name']) . '</b>. Not you? <a class="item" href="logout.php">Log out</a>';
}
?>
</div>
</div>
<div id="content">
And a Login.php with this code ( Here is the 4 lines of it ) :
<?php
include 'connect.php';
include 'header.php';
etc..... php code....
Ok so the problem is When i try to open the login.php in web browser i got the code in header.php duplication many many times like it don't end duplicating it self and if i open the source code of login.php i will got unlimited number of the code used in header.php like all the source code is header.php repeatedly.
So I'am asking you guys for help on how to fix this and what is the error ??
NOTE: Sorry if their was a thread duplication but i didn't know on what to search exactly.
If you want anymore information I'am ready.
Thanks all much appreciated
Use
include_once('header.php');
everywhere instead. It will check to see if the file has already been included.
use require_once instead, it will load the file just once, and only if needed.
You have more info here

How can I print a php variable from a session in the footer of an html/jquery page?

I currently am trying to use print inside of the JQuery footer by writing it is not working. Instead nothing shows up. In the earlier php page I stored it into the session using...
session_start();
$_SESSION['username'] = $_POST['username'];
my code in my html page is as follows...
<?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>Food For Thought</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.css" />
<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header" id = "hdrMain" name = "hdrMain" data-nobackbtn = "true">
<h1>Food for Thought</h1>
</div><!-- /header -->
<div data-role="content" align ="center">
Play
Profile
Logout
</div><!-- /content -->
<div data-role="footer">
<p>Logged in as: <?php print $_SESSION["username"]; ?> </p>
</div>
</div>
</body>
</html>
Thank you any help is appreciated.
Edit: Problem was that the file I was trying to run php code ended in .html instead of .php. Thank you all who tried to help.
Well, you're HTML page has:
session.start();
it should be
session_start();

Categories