Page 1 of 1
? Login Controled Pages ?
Posted: Wed Sep 25, 2002 11:17 am
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?
Posted: Wed Sep 25, 2002 11:21 am
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.
Posted: Wed Sep 25, 2002 12:46 pm
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?
Posted: Wed Sep 25, 2002 12:47 pm
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
Posted: Wed Sep 25, 2002 12:55 pm
by Zoram
I tried it and it still did the same thing.
Posted: Wed Sep 25, 2002 12:58 pm
by JPlush76
can you post the first 30-40 lines of code on your page exactly how you have them?
Posted: Wed Sep 25, 2002 1:00 pm
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";
}
Posted: Wed Sep 25, 2002 1:18 pm
by volka
If I take your code snippet and the error message I come to
Code: Select all
<?php
ob_start(); // turn output buffering on
session_start();
$db_user = <clipped>;
$db_pass = <clipped>;
$user_name = $_POSTї'user_name'];
$password = $_POSTї'password'];
/* <--- this is line 8, supposed to be the start of the offending output --->*/
//connect to the DB and select the "dictator" database /* <--- line 9, very odd --->*/
$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";
}
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
Posted: Wed Sep 25, 2002 2:41 pm
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();
.........
Posted: Wed Sep 25, 2002 3:58 pm
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
Posted: Wed Sep 25, 2002 4:07 pm
by Zoram
Thank you, that was the problem, i'm sorry for being such a dunce

Posted: Wed Sep 25, 2002 4:20 pm
by JPlush76
haha its ok
glad it worked