Home | Community | Message Board

Avalon Magic Plants
This site includes paid links. Please support our sponsors.


Welcome to the Shroomery Message Board! You are experiencing a small sample of what the site has to offer. Please login or register to post messages and view our exclusive members-only content. You'll gain access to additional forums, file attachments, board customizations, encrypted private messages, and much more!

Shop: Kraken Kratom Red Vein Kratom   Myyco.com Golden Teacher Liquid Culture For Sale

Jump to first unread post Pages: 1
Invisibleelbisivni
Registered: 10/01/06
Posts: 2,839
PHP simple login
    #7003449 - 06/03/07 02:05 PM (16 years, 10 months ago)

Trying to figure out a simple one for access to only one user, but am trying to take a somewhat secure route. With the code below I'm getting this error:
Code:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at -url-/index.php:12) in -url-/index.php on line 40


I've tried 4 or 5 different methods now but to no avail. If someone can peruse my code and point out any problems are point me in a better direction I'd really appreciate it.

BTW line 40 is "session_start();"

index.php
Code:
	<?
session_start();

if (isset($_SESSION['username'])){
$login=true;
} else {
$login=false;
}

if($login==false){
print("<table border=\"0\">
<tr><td>Username:</td><td>Password:<;/td><td></td></tr>
<tr><td><form name=\"login\" method=\"post\" action=\"user.php\"><input name=\"username\" type=\"text\" id=\"username\"></td>&lt;td><input name=\"password\" type=\"password\" id=\"password\"></td>&lt;td><input name=\"f\" type=\"hidden\" value=\"login\" /><input type=\"submit\" name=\"submit\" value=\"Log In\"></form></td></tr>
</table>");
}
if($login==true){
print("<a href=\"logout.php\">Log Out</a>");
}

include("connection.php");

...



user.php
(functions as login and logout based on hidden param submitted by form)
Code:
<?
include("connection.php");
$function=$_POST['f'];

if($function=="login"){
if(isset($submit)){
$result=mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$isAuth=false;
while($row=mysql_fetch_array($result)){
if($row['username']==$username){
$isAuth = true;
session_start();
session_register('username');
}
}
mysql_close($connection);
if($isAuth){
print "Login successful.<br><a href=\"index.php\">Return to Index</a>";
} else {
print "Login unsuccessful.";
}
}
}

if($function=="logout"){
session_start();
if(isset($_SESSION['phplogin'])){
unset($_SESSION['phplogin']);
}
header('Location: index.php');
exit();
}
?>



--------------------
From dust you are made and to dust you shall return.

Extras: Filter Print Post Top
Invisibleelbisivni
Registered: 10/01/06
Posts: 2,839
Re: PHP simple login [Re: elbisivni]
    #7003697 - 06/03/07 03:19 PM (16 years, 10 months ago)

I fixed the session_start(); error by placing it at the very top of the page. I worked on the scripts some more but still no beans. I submit the form to login on index.php, it moves to user.php, but always results in a failure.

index.php
Code:
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Music Index</title>
</head>
<body>
<?
include("connection.php");

if(!session_is_registered('member_id')){
$login=false;
} else {
$login=true;
}

if($login==false){
print("<table border=\"0\">
<tr><td>Username:</td><td>Password:<;/td><td></td></tr>
<tr><td><form name=\"login\" method=\"post\" action=\"user.php\"><input name=\"username\" type=\"text\" id=\"username\"></td>&lt;td><input name=\"password\" type=\"password\" id=\"password\"></td>&lt;td><input name=\"f\" type=\"hidden\" value=\"login\" /><input type=\"submit\" name=\"submit\" value=\"Log In\"></form></td></tr>
</table>");
}
if($login==true){
print("<a href=\"logout.php\">Log Out</a>");
}
...




user.php
Code:
<?
$function=$_POST['f'];

if($function=="login"){
session_start();
include("connection.php");
if(isset($_POST['submit'])){
$username=strip_tags($_POST['username']);
$password=sha1(strip_tags($_POST['password']));
$query=sprintf("SELECT id FROM users WHERE username='%s' AND password='%s' LIMIT 1;",mysql_real_escape_string($username),mysql_real_escape_string($password));
$result=mysql_query($query);
if(1!=mysql_num_rows($result)){
print("failure.");
} else {
$row=mysql_fetch_assoc($result);
$_SESSION['member_id']=$row['id'];
print("success.");
}
}
}

if($function=="logout"){
session_start();
if(true===session_unregister('member_id')){
print("logged out.");
} else {
unset($_SESSION['member_id']);
sleep(3);
print("logged out.");
}
}
?>



--------------------
From dust you are made and to dust you shall return.

Edited by elbisivni (06/03/07 03:34 PM)

Extras: Filter Print Post Top
Invisibleelbisivni
Registered: 10/01/06
Posts: 2,839
Re: PHP simple login [Re: elbisivni]
    #7004035 - 06/03/07 04:40 PM (16 years, 10 months ago)

never mind I figured it out, thanks for all of the help people :firecum:

:grin:


--------------------
From dust you are made and to dust you shall return.

Extras: Filter Print Post Top
Offlinedelta9
Active Ingredient
Male

Registered: 10/28/04
Posts: 5,390
Loc: California
Last seen: 13 years, 5 months
Re: PHP simple login [Re: elbisivni]
    #7006221 - 06/04/07 01:39 AM (16 years, 10 months ago)

PHP Session Reference

Sorry, was busy all weekend.  :smile:


--------------------
delta9

Extras: Filter Print Post Top
OfflineSeussA
Error: divide byzero


Folding@home Statistics
Registered: 04/27/01
Posts: 23,480
Loc: Caribbean
Last seen: 2 months, 7 days
Re: PHP simple login [Re: delta9]
    #7006407 - 06/04/07 04:40 AM (16 years, 10 months ago)

> Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at -url-/index.php:12) in -url-/index.php on line 40

This happens when you try to set a cookie after non-header data has been sent. With non-buffered output, a single new line before the first php tag can cause the problem. I know you already figured it out, but helping others in the future.


--------------------
Just another spore in the wind.

Extras: Filter Print Post Top
Jump to top Pages: 1

Shop: Kraken Kratom Red Vein Kratom   Myyco.com Golden Teacher Liquid Culture For Sale


Similar ThreadsPosterViewsRepliesLast post
* php easter egg Mycomancer 5,147 7 05/02/06 03:15 AM
by Le_Canard
* PHP NUKE RIPPING Genius 605 3 11/06/04 05:11 PM
by Ythan
* Stealther: IP Chaining Prog to login Shroomery Lana 3,565 10 08/23/02 04:08 PM
by Fd3000
* PHP vs. Cold Fusion Evolving 1,126 7 03/15/03 06:08 AM
by Lana
* Want to learn PHP: Book recommendations? Gr8fulJ420 961 6 03/09/06 09:39 AM
by Shdwstr
* PHP, MySQL Programmer (job offer in craigslist chicago!) ZippoZM 578 0 07/11/06 02:20 PM
by ZippoZ
* HELP ME WITH THIS SIMPLE XP PROBLEM !! Mykro_Guy 454 0 10/30/04 07:51 AM
by Mykro_Guy
* PHP coders wanted ThePredator 898 10 09/28/05 04:46 PM
by automan

Extra information
You cannot start new topics / You cannot reply to topics
HTML is disabled / BBCode is enabled
Moderator: trendal, automan, Northerner
1,372 topic views. 0 members, 1 guests and 1 web crawlers are browsing this forum.
[ Show Images Only | Sort by Score | Print Topic ]
Search this thread:

Copyright 1997-2024 Mind Media. Some rights reserved.

Generated in 0.019 seconds spending 0.007 seconds on 14 queries.