Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to protect .php page?
Old 12-13-2005, 06:06 PM how to protect .php page?
Novice Talker

Posts: 11
Location: Sofia
Trades: 0
Hi,
It must to protect 2 .php files with username and password. Which is the best way?
Thank you
Bimbas is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-13-2005, 07:41 PM
Bri
Bri's Avatar
Average Talker

Posts: 16
Location: Earth
Trades: 0
Hi Bimbas, do you need to be able to change your username and password when you want by 'editing' your profile once logged in? - Also, do you require the function to have users 'register'?
__________________
Why do snipers have hay and 5hit glued to their faces?
My site:
Please login or register to view this content. Registration is FREE
Bri is offline
Reply With Quote
View Public Profile Visit Bri's homepage!
 
Old 12-14-2005, 08:35 AM
Ultra Talker

Posts: 264
Location: UK
Trades: 3
I suggest a mysql database with username and passwords of your users. They then have to log in using these details to see your pages.

You need to use sessions and cookies.

These links will help.

http://www.websitepublisher.net/article/php_cookies/

http://phpbuilder.com/board/showthread.php?p=10680965
uktvindex is offline
Reply With Quote
View Public Profile
 
Old 12-14-2005, 10:05 AM Just One user
Novice Talker

Posts: 11
Location: Sofia
Trades: 0
The php page which i want to protect will have just 1 user(the owner of website). Tha page is with a form for administration
Bimbas is offline
Reply With Quote
View Public Profile
 
Old 12-14-2005, 12:08 PM
Bri
Bri's Avatar
Average Talker

Posts: 16
Location: Earth
Trades: 0
Hi, I will knock something together and post it here for you.
__________________
Why do snipers have hay and 5hit glued to their faces?
My site:
Please login or register to view this content. Registration is FREE
Bri is offline
Reply With Quote
View Public Profile Visit Bri's homepage!
 
Old 12-14-2005, 12:29 PM
stOx's Avatar
Machine

Latest Blog Post:
Worlds Smallest Car - Peel P50
Posts: 2,111
Name: Matt. (>',')>
Location: London, England.
Trades: 0
Have a form post the name and pass to this script.

PHP Code:
<?php

$name 
$_POST['name'];
$pass $_POST['pass'];

if(
$name == "yourname") && if($pass == "yourpass")
{
echo 
"woohoo correct";
}
else
{
echo 
"Wrong username and password";
}
?>
I wrote that off the top of my head, so don't assume it works, i didn't test it.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
stOx is offline
Reply With Quote
View Public Profile Visit stOx's homepage!
 
Old 12-14-2005, 01:18 PM
Ultra Talker

Posts: 264
Location: UK
Trades: 3
Just to warn you that code will only protect the one page, the protection will not be carried over subsequent pages linked from that page. If you just want to protect one page, then thats no problem!
uktvindex is offline
Reply With Quote
View Public Profile
 
Old 12-14-2005, 02:08 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
I would recommend using a .htaccess and .htpasswd file for this (assuming you run apache as the webserver). You can set this up to protect an entire folder of your webspace and all the child folders. It can be set up from the command line, or most CPanels have to option to password a folder.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 12-14-2005, 03:28 PM
Bri
Bri's Avatar
Average Talker

Posts: 16
Location: Earth
Trades: 0
Here you go:

change.php
PHP Code:
<?php
session_start
(); 
header("Cache-control: private");
// use this file to let users' change their data that is stored in 'data.txt'
// use your own html to integrate the script into your site
?>
<html>
<head>
<title>Change/Update Details</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<FORM action="login.php?action=change" method="post"> 
<p>Username: 
<?php echo $_SESSION['name']; ?>
</p>
<p>Name:
<INPUT name="name" type="text" value="<?php echo $_SESSION['realname']; ?>"> 
</p>
<p>Email: 
<INPUT name="email" type="text" value="<?php echo $_SESSION['email']; ?>">
</p>
<p>Url: 
<INPUT name="url" type="text" value="<?php echo $_SESSION['url']; ?>">
</p>
<p>Password:
<INPUT name="newpass" type="text" value="<?php echo $_SESSION['pass']; ?>">
<INPUT type="submit" value="Change"> 
</span> 
</p>
</FORM> 
</body>
</html>
login.php
PHP Code:
<?php
session_start
(); 
header("Cache-control: private");
?> 
<html> 
<head> 
<title>Logged-in</title> 
</head> 
<body> 
<?php
$datafile
="data.txt"//name of the data file
if($_GET['action'] == "login") { 
 
if(!isset(
$_POST['username']) || !isset($_POST['password'])){ //this checks that all the fields are filled in
 
error_message("One or more required fields were left blank!"$username$password); 

 
$file file($datafile); 
while(list(,
$value) = each($file)) { 
list(
$fname,$femail,$furl,$fuser,$fpass,$blank) = explode('|'$value); 
if(
$_POST['username'] == $fuser && $_POST['password'] == $fpass){ 
 
$_SESSION['name'] = $fuser;
$_SESSION['realname'] = $fname;
$_SESSION['url'] = $furl;
$_SESSION['email'] = $femail//adds the users data to the php session
$_SESSION['pass'] = $fpass;
 
//insert html for sucessfull log on here
//you can use . $_SESSION['name'] . for username
//replace name with realname, url, or email to display different name/field
?> 
you are logged in.<br />
view you profile <a href="profile.php">here</a><br />
or logout <a href="logout.php">here</a>.
</body>
</html>
 
<?php
$logink 
true


if(
$logink !== true) { 
?>
Login failed, bad username/password
<?

} elseif(
$_GET['action'] == 'change') { 
 
if(!isset(
$_POST['name']) || !isset($_POST['email']) || !isset($_POST['url']) || !isset($_POST['newpass'])) { 
 
//this checks that all the fields are filled in
?>
all fields were not filled in
<?

 
$file file($datafile); 
while(list(,
$value) = each($file)){ 
list(
$fname,$femail,$furl,$fuser,$fpass) = explode('|'$value); 
if(
$_SESSION['name'] == $fuser){ 
//opens data file to change it
$oldword "$fname|$femail|$furl|$fuser|$fpass|"
$newword $_POST['name'] . '|' $_POST['email'] . '|' $_POST['url'] . '|' $_SESSION['name'] . '|' $_POST['newpass'] . '|'
$fp fopen($datafile'r'); 
$data fread($fpfilesize($datafile)); 
fclose($fp); 
$newdata str_replace($oldword$newword$data); 
$fp fopen($datafile'w'); 
fwrite($fp,$newdata) or die ('error writing'); 
fclose($fp); 
$succ true//data changed sucessfully
?> 
Everything was changed successfully please <a href="user.php">login</a>
<?
$_SESSION
['name'] = FALSE
$_SESSION['pass'] = FALSE
$_SESSION['url'] = FALSE
$_SESSION['realname'] = FALSE
$_SESSION['email'] = FALSE


if(
$succ !== true) { 
?>
Login failed, bad username/password
<?

} else { 
exit; 

?>
logout.php
PHP Code:
<?php
session_start
(); 
header("Cache-control: private");
// html for logging out
?>
<html> 
<head>
<title>Logout</title>
</head>
<body>
 
<?php 
if(!$_SESSION['name']){ 
echo 
"you are not logged in"// This is what will be displayed of no session data is found
} else { 
$_SESSION['name'] = FALSE
$_SESSION['pass'] = FALSE//this deletes the session data from php
$_SESSION['url'] = FALSE
$_SESSION['realname'] = FALSE
$_SESSION['email'] = FALSE
echo 
"you have been logged out"

?> 
</body>
</html>
profile.php
PHP Code:
<?php
session_start
(); 
header("Cache-control: private");
// use your own html to integrate the script into your site
?>
<html>
<head>
<title>Users Profile</title>
</head>
<body>
<?php
if(!$_SESSION['name']){
 
echo 
"you are not logged in"// This is what will be displayed of no session data is found
} else { 
echo 
"Username: " $_SESSION['name'] . "<br />"//this displays the information in the php session
echo "Name: " $_SESSION['realname'] . "<br />"//like a profile
echo "Url: <a href='" $_SESSION['url'] . "'>" $_SESSION['url'] . "</a><br />";
echo 
"Email: " $_SESSION['email'] . "<br />";
echo 
"Pass: " $_SESSION['pass'] . "<br />";
?>
<a href="change.php">Edit you profile </a>
<?php

?>
</body>
</html>
signup.php
PHP Code:
<html> 
<head> 
<title>Register</title> 
</head> 
<body> 
<?php 
$datafile 
'data.txt'
if(
$_GET['action'] == 'signup') { 
 
if(!isset(
$_POST['name']) || !isset($_POST['email']) || !isset($_POST['url']) || !isset($_POST['username']) || !isset($_POST['password'])){ 
 
error_message('One or more required fields were left blank!'$_POST['name'], $_POST['email'], $_POST['url'], $_POST['username'], $_POST['password']); 

 
$file file($datafile); 
while(list(,
$value)=each($file)){ //check if user exists
list($fname,$femail,$furl,$fuser,$fpass,$blank)=split"\|"$value); 
if(
$username==$fuser){ 
 
error_message('Username is allready in use.'$_POST['name'], $_POST['email'], $_POST['url'], $_POST['username'], $_POST['password']); 


 
$fp fopen($datafile'a'); 
fwrite($fp$_POST['name'] . '|' $_POST['email'] . '|' $_POST['url'] . '|' $_POST['username'] . '|' $_POST['password'] . "|\n"); 
fclose($fp); 
//html for sucessfull signup
?> 
You have been sucessfulle registered.<br />
You may now <a href="user.php">login</a> using the followin details<br /> 
username: <?echo $username;?><br /> 
password: <?echo $password;?> 
</body>
</html>
<?php 
} else { 
//html for sign up
?> 
<html>
<head>
<title>Register</title>
</head>
<body>
 
<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>?action=signup" method="post"> 
<p>Name: 
<INPUT type="text" name="name"></p> 
<p>E-mail Address: 
<INPUT type="text" name="email"></p>
<p>Website Address: 
<INPUT type="text" name="url"></p>
<p>Desired Username: 
<INPUT type="text" name="username"></p>
<p>Password: 
<INPUT type="password" name="password"></p>
<INPUT type="submit" value="Sign-up"> 
</FORM> 
</body>
</html>
 
<?php 

function 
error_message($message$name$email$url$username$password) { 
?> 
<html>
<head> //html for signing up error
<title>Error</title>
</head>
<body>
<?php echo $message;?> //error message
<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>?action=signup" method="post">
 
<P>Name: <INPUT type="text" name="name" value="<?php echo $name;?>"> 
<P>E-mail Address: <INPUT type="text" name="email" value="<?php echo $email;?>"> 
<P>Website Address: <INPUT type="text" name="url" value="<?php echo $url;?>"> 
<P>Desired Username: <INPUT type="text" name="username" value="<?php echo $username;?>">
 
<P>Password: <INPUT type="password" name="password" value="<?php echo $password;?>"> 
<P><INPUT type="submit" value="Sign-up"> 
</FORM> 
</body>
</html>
 
<?php 
exit; 

?> 
</body> 
</html>
user.php
PHP Code:
<html>
<
head>
<
title>User</title>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</
head>
<
body>
<
form name="login" method="post" action="login.php?action=login">
<
p>
Username: <input type="text" name="username">
</
p>
<
p>
Password
<
input type="password" name="password">
</
p>
<
p>
<
input type="submit" name="Submit" value="Login">
</
p>
<
p>havent got a username register <a href="signup.php">here</a></p>
</
form>
</
body>
</
html
Also create a blank .txt file called data.txt

Edit all the above HTML found within each file to your liking to integrate the script into your site.

NOTE:
You must chmod data.txt to 777
Files Included:
logout.php - Script to alow users to logout
signup.php - Script for users to register
profile.php - Displays users data
change.php - Alows users to change their data
login.php - Main php file
data.txt - File containing users data
users.php - Allows users to login

Simply run user.php to test the script out. Hope this does what you want.

Bri
__________________
Why do snipers have hay and 5hit glued to their faces?
My site:
Please login or register to view this content. Registration is FREE

Last edited by Bri; 12-14-2005 at 03:49 PM..
Bri is offline
Reply With Quote
View Public Profile Visit Bri's homepage!
 
Old 12-14-2005, 05:32 PM
Novice Talker

Posts: 11
Location: Sofia
Trades: 0
I'm looking for something like stOx's script, but it don't works
Bimbas is offline
Reply With Quote
View Public Profile
 
Old 12-14-2005, 05:41 PM
Ultra Talker

Posts: 264
Location: UK
Trades: 3
Hugely comprehensive post and yet unappreciated post Bri. I have added to your talkputation.

I will come to you if I have any problems!
uktvindex is offline
Reply With Quote
View Public Profile
 
Old 12-14-2005, 07:23 PM
Bri
Bri's Avatar
Average Talker

Posts: 16
Location: Earth
Trades: 0
Sorry my script was not what you had in mind Bimbas At least someone found it of use though eh

Thanks uktvindex
__________________
Why do snipers have hay and 5hit glued to their faces?
My site:
Please login or register to view this content. Registration is FREE
Bri is offline
Reply With Quote
View Public Profile Visit Bri's homepage!
 
Old 12-14-2005, 07:25 PM
Bri
Bri's Avatar
Average Talker

Posts: 16
Location: Earth
Trades: 0
P.S.
Anyone wanting a demo of what the above code actually does can have a look here.
__________________
Why do snipers have hay and 5hit glued to their faces?
My site:
Please login or register to view this content. Registration is FREE
Bri is offline
Reply With Quote
View Public Profile Visit Bri's homepage!
 
Old 12-15-2005, 03:12 AM
Novice Talker

Posts: 11
Location: Sofia
Trades: 0
Hi to all, It seems that the problem is on my hosting provider, i'll talk with them now.
Bri, your script is very very useful, and I'll use it in some other site. Thank you.
Bimbas is offline
Reply With Quote
View Public Profile
 
Old 12-15-2005, 10:31 AM
Bri
Bri's Avatar
Average Talker

Posts: 16
Location: Earth
Trades: 0
I am glad you liked it even though it is a little rough around the edges

Hope you get things sorted out
__________________
Why do snipers have hay and 5hit glued to their faces?
My site:
Please login or register to view this content. Registration is FREE
Bri is offline
Reply With Quote
View Public Profile Visit Bri's homepage!
 
Reply     « Reply to how to protect .php page?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45976 seconds with 12 queries