? Login Controled Pages ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

? Login Controled Pages ?

Post by Zoram »

I am trying to figure out how to setup a login system so that people have to login to access certain pages. I want it so that when they go to a page, say to add an article, they have to be logged in and if not then it takes them to a login page.

This will only be for admin so i don't need to make it so they can register, just so that it will check their username and password against the one in the database. how would i do this?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

For starter's see this excellent tutorial for the login portion. Once you understand how it works, we can probably help you figure out how to use it to protect only certain pages, etc.
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

k, i've tried the tutorial and this is what i get when i try to run it :

Code: Select all

Warning: Cannot send session cookie - headers already sent by (output started at /home/navigato/public_html/Admin/validate.php:8) in /home/navigato/public_html/Admin/validate.php on line 9

Warning: Cannot send session cache limiter - headers already sent (output started at /home/navigato/public_html/Admin/validate.php:8) in /home/navigato/public_html/Admin/validate.php on line 9
error making query
How can i fix it?
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

put this at the VERY top of your php script

Code: Select all

<?php
ob_start(); // turn output buffering on 
?>
that will fix it
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

I tried it and it still did the same thing.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

can you post the first 30-40 lines of code on your page exactly how you have them?
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

<?php
ob_start(); // turn output buffering on
session_start();
$db_user = <clipped>;
$db_pass = <clipped>;
$user_name = $_POST['user_name'];
$password = $_POST['password'];

//connect to the DB and select the "dictator" database
$connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error());
mysql_select_db('navigato_Directory', $connection) or die(mysql_error());

//set up the query
$query = "SELECT * FROM users
WHERE user_name='$user_name' AND password='$password'";

//run the query and get the number of affected rows
$result = mysql_query($query, $connection) or die('error making query');
$affected_rows = mysql_num_rows($result);

//if there's exactly one result, the user is validated. Otherwise, he's invalid
if($affected_rows == 1) {
$data_block = "Valid Login";
//add the user to our session variables
$_SESSION['username'] = $user_name;
}
else {
$data_block = "Invalid Login";
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If I take your code snippet and the error message I come to

Code: Select all

&lt;?php 
ob_start(); // turn output buffering on 
session_start(); 
$db_user = &lt;clipped&gt;; 
$db_pass = &lt;clipped&gt;; 
$user_name = $_POST&#1111;'user_name']; 
$password = $_POST&#1111;'password']; 
   /* &lt;---  this is line 8, supposed to be the start of the offending output ---&gt;*/
//connect to the DB and select the "dictator" database /* &lt;--- line 9, very odd ---&gt;*/
$connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error()); 
mysql_select_db('navigato_Directory', $connection) or die(mysql_error()); 

//set up the query 
$query = "SELECT * FROM users 
WHERE user_name='$user_name' AND password='$password'"; 

//run the query and get the number of affected rows 
$result = mysql_query($query, $connection) or die('error making query'); 
$affected_rows = mysql_num_rows($result); 

//if there's exactly one result, the user is validated. Otherwise, he's invalid 
if($affected_rows == 1) { 
$data_block = "Valid Login"; 
//add the user to our session variables 
$_SESSION&#1111;'username'] = $user_name; 
} 
else { 
$data_block = "Invalid Login"; 
}
can you please post an up2date error message and the code snippet of validate.php where lines mentioned in the error message(s) are marked?
Otherwise it will be more than tricky to figure out what happens
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

Sorry about that, there was a few lines of html coding before it. It showed that it was on the line with the: ob_start();

the code for the error in line 8 is:

Code: Select all

<html>
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Navigator :: Family Travel Magazine</title>
<!-- InstanceEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="head" -->
<?php
	ob_start(); // turn output buffering on
	session_start();
.........
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

yea I said you need that ob_start() at the VERY top of your page, that meant before any HTML or anything

the very first line of your entire page should be the ob_start
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

Thank you, that was the problem, i'm sorry for being such a dunce :oops:
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

haha its ok :)

glad it worked
Post Reply