/    Sign up×
Community /Pin to ProfileBookmark

my attempt of a login script problems?

iv decided to try to write myself a login script and all was going well until i tried to create a function to check if the user has logged in already.

basically if there logged in continue otherwise dont show the page and ask the user to login.

i dont seem to get on with functions and i dont really understand them altho i have looked up about them.

at the moment im getting this error on login:

Warning: Cannot modify header information – headers already sent by (output started at /home/john5115/public_html/site3/connections/sql_db_connection.php:2) in /home/john5115/public_html/site3/includes/login.php on line 31

Warning: Cannot modify header information – headers already sent by (output started at /home/john5115/public_html/site3/connections/sql_db_connection.php:2) in /home/john5115/public_html/site3/includes/login.php on line 32

also should you include the users encrypted password in a cookie for a login system?

[url]http://www.knowj.com/site3/?id=7[/url]
login.php

[code=php]
<?
require_once “includes/functions.php”;

if ($_POST[‘action’] == ‘submitted’)
{
//set the globals as variables
$login = $_POST[’email’];
$password = $_POST[‘password’];

//unset the globlas
unset($_POST[’email’], $_POST[‘password’]);

//encrypt the data
$encrypted = /*md5(sha1(*/$password/*))*/;
//unset the $password variable
unset($password, $db_pass);

$query = “SELECT username, password
FROM kj_member
WHERE username = ‘$login'”;
$result = mysql_query($query);
$password_check = mysql_fetch_array($result);

$db_pass = $password_check[‘password’];
$db_login = $password_check[‘username’];
unset($password_check);

if ($encrypted === $db_pass)
{
setcookie(“kj_usr_log”, $db_login, time()+3600, “/”, “www.knowj.com”);
setcookie(“kj_usr_ps”, $db_pass, time()+3600, “/”, “www.knowj.com”);
unset($db_pass, $db_login);
echo “<h1>Connected www.knowj.com – Member Area</h1>”;
require_once “includes/members.php”;
}
else
{
unset($db_pass, $db_login);
echo “<h1>Invalid Username or Password</h1>”;
$email = ‘[email protected]’;
//set the var for the clients message
$message = “Somone tried to access with in invalid login and or password on client ip:” . $_SERVER[‘HTTP_CLIENT_IP’] . “rn”;
//email header
$headers = ‘From: [email protected]’ . “rn”;
//mail the security alert
mail($email, ‘invalid login’, $message, $headers);
}
}
else
{
?>
<div style=”width:50%;”>
<h1>www.knowj.com – Member Login:</h1>
<form name=”register” method=”post” action=”?id=7″>
<fieldset>
<legend>Login:</legend>
<label>Email:</label><br>
<input type=”text” name=”email” size=”50″>
<br>
<label>Password:</label><br>
<input type=”password” name=”password” size=”50″>
<p class=”float-right”><input type=”hidden” name=”action” value=”submitted”><input type=”submit” name=”Submit” value=”Login” style=”font-size:1em;”></p>
</fieldset>

</form>
<a style=”color:#F17416″ href=”#”>forgot password</a>
</div>
<?
}
?>
[/code]

functions.php

[code=php]
<?
function login_check()
{
if (isset($_COOKIE[‘kj_usr_log’]))
{
$loginnm = $_COOKIE[‘kj_usr_log’];
$query = “SELECT username, password
FROM kj_member
WHERE username = ‘$loginnm'”;
$result = mysql_query($query);
$password_check = mysql_fetch_array($result);

$db_pass = $password_check[‘password’];
$db_login = $password_check[‘username’];
unset($password_check);

if ($encrypted === $db_pass)
{
unset($db_pass, $db_login);
echo “<h1>Connected www.knowj.com – Member Area</h1>”;
}
else
{
echo “you are not logged in please login to view this page”;
die;
}
}
}
?>
[/code]

members.php

[code=php]
<?
require_once “includes/functions.php”;
if (login_check() == TRUE)
{
?>
<p>member area</p>
<?
}
else
{
echo “you need to login”;
}
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guyFeb 25.2006 — The error Warning: Cannot modify header information - headers already sent by (output started at /home/john5115/public_html/site3/connections/sql_db_connection.php:2) in /home/john5115/public_html/site3/includes/login.php on line 31

Warning: Cannot modify header information - headers already sent by (output started at /home/john5115/public_html/site3/connections/sql_db_connection.php:2) in /home/john5115/public_html/site3/includes/login.php on line 32
is because you may be trying to set a cookie or send some other header information after some output has been already sent to the browser. What you need to make sure is that firstly there are no spaces before your open php tag <?php in the document, second make sure that no information is being echoed out before the cookies are set. You can use [url=http://au.php.net/manual/en/function.ob-start.php]ob_start()[/url] in the first line in the script and then [url=http://au.php.net/manual/en/function.ob-flush.php]ob_flush()[/url] at the end
×

Success!

Help @knowj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...