Web Design Forum: PHP Webpage Login - Web Design Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP Webpage Login Please help me adapt this script Rate Topic: -----

#1 User is offline   caraewilton 

  • Advanced Member
  • PipPipPip
  • View blog
  • Group: Members
  • Posts: 364
  • Joined: 24-October 08
  • Location:South Africa
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 23 June 2009 - 08:00 PM

Hi guys. Need a little help adapting this php script. Basically I am helping a church update their website. They want a few pages to be password protected.

I found the following script which works does the job, except now I have been asked if it is possible to set up things so that within a browser session, the password is "remembered" so that people won't have to re-enter the password if the browse away from the protected page and then go back again.

This is a little out of my league as I am new to php so I was hoping someone here could help me add the required code.

Here is what I have so far:

<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Members Page</title>
<?php $password = "admin";?>

</head>

<body>
<div id="top">
	<div id="header"></div>
</div>
<div id="menu">
	<ul>
		<li><a href="#nogo">Home</a></li>
		<li><a href="#nogo">Sunday Services</a></li>
		<li><a href="#nogo">Activities</a></li>
		<li><a href="#nogo">Gallery</a></li>
		<li><a href="#nogo">Community</a></li>
		<li><a href="#nogo">Youth</a></li>
		<li><a href="#nogo">Letting / Room hire</a></li>
		<li><a href="#nogo">Contact</a></li>	
	</ul>
</div>
<div id="content">
	<div id="content-wrapper">
		<div id="left">
			<div class="inner-wrapper">
				<h2>Members Page</h2>
				<?php 
				if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
				?>
				<!-- START OF HIDDEN CONTENT -->
				
				  <p>Congratulations you have gained access to the Protected and Secret Area!</p>
				
				<!-- END -->
				<?php 
				}
				else
				{
				if (isset($_POST['password']) || $password == "") {
				  print "<h4>Incorrect Password</h4><h4>Please enter the correct password</h4>";}
				  print "<form method=\"post\" action=\"\"><p>Please enter your password for access<br />";
				  print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\" /><input value=\"Login\" type=\"submit\" /></p></form>";
				}
				?>
			</div>
		</div>
		<div id="right">
			<div class="inner-wrapper"></div>
		</div>
		<div id="content-footer"></div>
	</div>
</div>
<div id="footer"></div>

</body>
</html>


Here is a link to the demo page as it is working now. LINK

Thanks
Cara
0

#2 User is offline   Meow 

  • Advanced Member
  • PipPipPip
  • View blog
  • View gallery
  • Group: Members
  • Posts: 476
  • Joined: 06-April 09
  • Gender:Female
  • Location:Rugby
  • Interests:CSI:NY, reading (mostly crime fiction and real-life crime), cooking, running a Brownie unit, the smell of printers ink
  • Experience:Intermediate
  • Area of Expertise:Designer

Posted 23 June 2009 - 08:12 PM

Sounds like maybe you want to set a cookie?
Session cookies will last the length of the browser session, or persistent/permanent cookies are stored for the next visit(s).
0

#3 User is offline   caraewilton 

  • Advanced Member
  • PipPipPip
  • View blog
  • Group: Members
  • Posts: 364
  • Joined: 24-October 08
  • Location:South Africa
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 23 June 2009 - 08:14 PM

View Postcinnamondm, on Jun 23 2009, 22:12, said:

Sounds like maybe you want to set a cookie?
Session cookies will last the length of the browser session, or persistent/permanent cookies are stored for the next visit(s).

Yes exactly, but how do I do this?
0

#4 User is offline   Faevilangel 

  • Wordpress Ninja.....
  • PipPipPipPip
  • View blog
  • Group: Members
  • Posts: 993
  • Joined: 11-May 09
  • Gender:Male
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 23 June 2009 - 08:31 PM

all available here
0

#5 User is offline   caraewilton 

  • Advanced Member
  • PipPipPip
  • View blog
  • Group: Members
  • Posts: 364
  • Joined: 24-October 08
  • Location:South Africa
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 23 June 2009 - 08:35 PM

View PostFaevilangel, on Jun 23 2009, 22:31, said:

all available here

Thanks but I really don't know how to apply this to my code.
0

#6 User is offline   notbanksy 

  • run away
  • PipPipPipPipPip
  • View blog
  • View gallery
  • Group: Members
  • Posts: 3,315
  • Joined: 14-February 08
  • Gender:Male
  • Location:Darkest rural Devon...
  • Interests:Cycling, dogs, making music
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 23 June 2009 - 09:24 PM

Something like this.
Once the user enters the correct username and password, use the info to set a session:
<?php
//get username and password from form
$username=$_POST['username'];
$password=$_POST['password'];

//get data from database
$sql=”SELECT * FROM logins WHERE username=’$username’ and password=’$password’”;
$result=mysql_query($sql);

//mysql num row is counting table row
$count=mysql_num_rows($result);
//if results match, then $count=1

if($count==1){
//register username and password, redirect to login_success.php
session_register(”username”);
session_register(”password”);
header(”location:password_protected_page.php”);
}
else{
echo ‘<a href=”login.php”>Access Denied. You have brought shame on your family, click here to go back</a>’;
}
?>

The on all susequent pages containing restricted content, use the following to filter the users out:
<?php
//check if session is registered, if not then redirect back to main page
session_start();
if(!session_is_registered(username)){
header(”location:login.php”);
}
//rest of the page goes here
?>

This works for me :)
Bloomin' good web design and copywriting from a branch of design

Refreshing web design articles and more

Microsoft is not the answer. Microsoft is the question. NO, or Linux, is the answer.
0

#7 User is offline   CodeWriter 

  • Dedicated Member
  • PipPip
  • View blog
  • Group: Members
  • Posts: 104
  • Joined: 03-February 09
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Nothing

Posted 23 June 2009 - 09:26 PM

Do you still need some help coding this?
0

#8 User is offline   caraewilton 

  • Advanced Member
  • PipPipPip
  • View blog
  • Group: Members
  • Posts: 364
  • Joined: 24-October 08
  • Location:South Africa
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 24 June 2009 - 03:47 AM

View PostCodeWriter, on Jun 23 2009, 23:26, said:

Do you still need some help coding this?

yes please
0

#9 User is offline   Jack Ellis 

  • Forum Newcomer
  • Pip
  • View blog
  • Group: Members
  • Posts: 35
  • Joined: 26-June 09
  • Gender:Male
  • Location:West Sussex
  • Interests:PHP, HTML, CSS, Javascript (jQuery Library)
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 26 June 2009 - 12:45 PM

<?php

session_start();

?>

<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Members Page</title>
<?php 

$password = "admin";?>

</head>

<body>
<div id="top">
	<div id="header"></div>
</div>
<div id="menu">
	<ul>
		<li><a href="#nogo">Home</a></li>
		<li><a href="#nogo">Sunday Services</a></li>
		<li><a href="#nogo">Activities</a></li>
		<li><a href="#nogo">Gallery</a></li>
		<li><a href="#nogo">Community</a></li>
		<li><a href="#nogo">Youth</a></li>
		<li><a href="#nogo">Letting / Room hire</a></li>
		<li><a href="#nogo">Contact</a></li>	
	</ul>
</div>
<div id="content">
	<div id="content-wrapper">
		<div id="left">
			<div class="inner-wrapper">
				<h2>Members Page</h2>
				<?php
				if ((isset($_POST["password"]) && ($_POST["password"]=="$password")) | $_SESSION['password'] == 'var') {
					
					$_SESSION['password'] = 'var';
				?>
				<!-- START OF HIDDEN CONTENT -->
				
				  <p>Congratulations you have gained access to the Protected and Secret Area!</p>
				
				<!-- END -->
				<?php
				}
				else
				{
				if (isset($_POST['password']) || $password == "") {
				  print "<h4>Incorrect Password</h4><h4>Please enter the correct password</h4>";}
				  print "<form method=\"post\" action=\"\"><p>Please enter your password for access<br />";
				  print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\" /><input value=\"Login\" type=\"submit\" /></p></form>";
				}
				?>
			</div>
		</div>
		<div id="right">
			<div class="inner-wrapper"></div>
		</div>
		<div id="content-footer"></div>
	</div>
</div>
<div id="footer"></div>

</body>
</html>


That works on my localhost. Just added a small bit into your procedural coding :D
Check My Profile if you are interested in my services or have a question for me.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users