Problemer med login

7 innlegg i emnet

Skrevet

Jeg har problemer med login koden, får feilmelding.

Alt er bare under testing, men vil få det til å fungere.

Her er all koden.

Login koden:

<?php

	require_once ("functions.php");

	require_once ("mysql_connect.php"); // Connect to the database


	//print '<link rel="stylesheet" type="text/css" href="mystyle.css" />

	//<div id="container">';

	include ("header.php");

	include ("navigation.php");

	print '<div id="main">';



// Connects to your Database



//Checks if there is a login cookie


if(isset($_COOKIE['ID_my_site']))



//if there is, it logs you in and directes you to the members page

{ 

	$username = $_COOKIE['ID_my_site']; 

	$pass = $_COOKIE['Key_my_site'];


	$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());


	while($info = mysql_fetch_array( $check ))  

	{


  if ($pass != $info['password']) 

  {

 	 

  }


  else

  {

 	 header("Location: contents2.php?id=6");

  }


	}


}



//if the login form is submitted


if (isset($_POST['submit'])) { // if form has been submitted



// makes sure they filled it in


	if(!$_POST['username'] | !$_POST['pass']) {

  die('You did not fill in a required field.');

	}


	// checks it against the database


	if (!get_magic_quotes_gpc()) {

  $_POST['email'] = addslashes($_POST['email']);

	}


	$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());


//Gives error if user dosen't exist


$check2 = mysql_num_rows($check);

if ($check2 == 0) {

  die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');

    }



while($info = mysql_fetch_array( $check ))  

{


$_POST['pass'] = stripslashes($_POST['pass']);

	$info['password'] = stripslashes($info['password']);

	$_POST['pass'] = md5($_POST['pass']);


//gives error if the password is wrong


	if ($_POST['pass'] != $info['password']) {

  die('Incorrect password, please try again.');

	}


else

{

// if login is ok then we add a cookie 


$_POST['username'] = stripslashes($_POST['username']);



$hour = time() + 3600; 

setcookie(ID_my_site, $_POST['username'], $hour);

setcookie(Key_my_site, $_POST['pass'], $hour);	


//then redirect them to the members area

//header("Location: members.php");

header("Location: contents2.php?id=5");

}


}


} else {	


// if they are not logged in

?>


<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">

<table border="0">

<tr><td colspan=2><h1>Login</h1></td></tr>

<tr><td>Username:</td><td>

<input type="text" name="username" maxlength="40">

</td></tr>

<tr><td>Password:</td><td>

<input type="password" name="pass" maxlength="50">

</td></tr>

<tr><td colspan="2" align="right">

<input type="submit" name="submit" value="Login">

</td></tr>

</table>

</form>

<?php

}



	print '</div>';


	include ("footer.php");


	//print '</div>';

?>
Feilmelding:
Warning: Cannot modify header information - headers already sent by (output started at /var/www/php/Forum/new try/login.php:6) in /var/www/php/Forum/new try/logintest.php on line 82


Warning: Cannot modify header information - headers already sent by (output started at /var/www/php/Forum/new try/login.php:6) in /var/www/php/Forum/new try/logintest.php on line 83


Warning: Cannot modify header information - headers already sent by (output started at /var/www/php/Forum/new try/login.php:6) in /var/www/php/Forum/new try/functions.php on line 51

Har sett etter blank-spaces i slutten av alle php filene.

Vet ikke hva jeg skal gjøre.

Kan jeg sette header og setcookie noe annet sted?

Hjelp tas imot med takk.

Mvh

ElBarto

0

Del dette innlegget


Lenke til innlegg
Del på andre sider

Skrevet

du kan prøve å buffre outputtet med ob_start(); helt i begynnelsen og

ob_flush(); helt i slutten.

og pass på at du ikke har noe output før du skal bruke headerene

0

Del dette innlegget


Lenke til innlegg
Del på andre sider

Skrevet

du kan prøve å buffre outputtet med ob_start(); helt i begynnelsen og

ob_flush(); helt i slutten.

og pass på at du ikke har noe output før du skal bruke headerene

Takk for hjelp, men det funket ikke.

Hmm.. vet ikke helt om jeg har noe output føre jeg bruker headerene. :S

Kan egentlig svært lite php, men vi har en oppgave vi skal innlevere, så da må jeg få det til.

0

Del dette innlegget


Lenke til innlegg
Del på andre sider

Skrevet

Etter det jeg har erfart, kan du forsøke med denne. Lim inn denne eksakt sånn som jeg har endret den, så forsøker du.

<?php

require_once ("functions.php");

require_once ("mysql_connect.php");

//print '<link rel="stylesheet" type="text/css" href="mystyle.css" />

//<div id="container">';

include ("header.php");

include ("navigation.php");

print '<div id="main">';

?>



<?php

// Connects to your Database



//Checks if there is a login cookie


if(isset($_COOKIE['ID_my_site']))



//if there is, it logs you in and directes you to the members page

{

$username = $_COOKIE['ID_my_site'];

$pass = $_COOKIE['Key_my_site'];


$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());


while($info = mysql_fetch_array( $check ))  

{


 if ($pass != $info['password'])

 {


 }


 else

 {

  header("Location: contents2.php?id=6");

 }


}


}



//if the login form is submitted


if (isset($_POST['submit'])) { // if form has been submitted



// makes sure they filled it in


if(!$_POST['username'] | !$_POST['pass']) {

 die('You did not fill in a required field.');

}


// checks it against the database


if (!get_magic_quotes_gpc()) {

 $_POST['email'] = addslashes($_POST['email']);

}


$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());


//Gives error if user dosen't exist


$check2 = mysql_num_rows($check);

if ($check2 == 0) {

 die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');

   }



while($info = mysql_fetch_array( $check ))  

{


$_POST['pass'] = stripslashes($_POST['pass']);

$info['password'] = stripslashes($info['password']);

$_POST['pass'] = md5($_POST['pass']);


//gives error if the password is wrong


if ($_POST['pass'] != $info['password']) {

 die('Incorrect password, please try again.');

}


else

{

// if login is ok then we add a cookie


$_POST['username'] = stripslashes($_POST['username']);



$hour = time() + 3600;

setcookie(ID_my_site, $_POST['username'], $hour);

setcookie(Key_my_site, $_POST['pass'], $hour);


//then redirect them to the members area

//header("Location: members.php");

header("Location: contents2.php?id=5");

}


}


} else {


// if they are not logged in

?>


<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">

<table border="0">

<tr><td colspan=2><h1>Login</h1></td></tr>

<tr><td>Username:</td><td>

<input type="text" name="username" maxlength="40">

</td></tr>

<tr><td>Password:</td><td>

<input type="password" name="pass" maxlength="50">

</td></tr>

<tr><td colspan="2" align="right">

<input type="submit" name="submit" value="Login">

</td></tr>

</table>

</form>

<?php

}



print '</div>';


include ("footer.php");


//print '</div>';

?>

0

Del dette innlegget


Lenke til innlegg
Del på andre sider

Skrevet

Tror det er denne som er feilen: print '<div id="main">';

Enn hvis du prøver include istedefor header eller tar bort print '<div id="main">';?

<?php

require_once ("functions.php");

require_once ("mysql_connect.php"); // Connect to the database


//print '<link rel="stylesheet" type="text/css" href="mystyle.css" />

//<div id="container">';

include ("header.php");

include ("navigation.php");

//print '<div id="main">';



// Connects to your Database



//Checks if there is a login cookie


if(isset($_COOKIE['ID_my_site']))



//if there is, it logs you in and directes you to the members page

{

$username = $_COOKIE['ID_my_site'];

$pass = $_COOKIE['Key_my_site'];


$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());


while($info = mysql_fetch_array( $check ))  

{


 if ($pass != $info['password'])

 {


 }


 else

 {

  //include("contents2.php?id=6");

  header("Location: contents2.php?id=6");

 }


}


}



//if the login form is submitted


if (isset($_POST['submit'])) { // if form has been submitted



// makes sure they filled it in


if(!$_POST['username'] | !$_POST['pass']) {

 die('You did not fill in a required field.');

}


// checks it against the database


if (!get_magic_quotes_gpc()) {

 $_POST['email'] = addslashes($_POST['email']);

}


$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());


//Gives error if user dosen't exist


$check2 = mysql_num_rows($check);

if ($check2 == 0) {

 die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');

   }



while($info = mysql_fetch_array( $check ))  

{


$_POST['pass'] = stripslashes($_POST['pass']);

$info['password'] = stripslashes($info['password']);

$_POST['pass'] = md5($_POST['pass']);


//gives error if the password is wrong


if ($_POST['pass'] != $info['password']) {

 die('Incorrect password, please try again.');

}


else

{

// if login is ok then we add a cookie


$_POST['username'] = stripslashes($_POST['username']);



$hour = time() + 3600;

setcookie(ID_my_site, $_POST['username'], $hour);

setcookie(Key_my_site, $_POST['pass'], $hour);


//then redirect them to the members area

//header("Location: members.php");

//include("contents2.php?id=5");

header("Location: contents2.php?id=5");

}


}


} else {


// if they are not logged in

?>


<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">

<table border="0">

<tr><td colspan=2><h1>Login</h1></td></tr>

<tr><td>Username:</td><td>

<input type="text" name="username" maxlength="40">

</td></tr>

<tr><td>Password:</td><td>

<input type="password" name="pass" maxlength="50">

</td></tr>

<tr><td colspan="2" align="right">

<input type="submit" name="submit" value="Login">

</td></tr>

</table>

</form>

<?php

}



//print '</div>';


include ("footer.php");


//print '</div>';

?>

0

Del dette innlegget


Lenke til innlegg
Del på andre sider

Skrevet

Det fungerte ikke det heller. :(

Warning: Cannot modify header information - headers already sent by (output started at /var/www/php/Forum/login.php:5) in /var/www/php/Forum/login.php on line 94


Warning: Cannot modify header information - headers already sent by (output started at /var/www/php/Forum/login.php:5) in /var/www/php/Forum/login.php on line 95


Warning: Cannot modify header information - headers already sent by (output started at /var/www/php/Forum/login.php:5) in /var/www/php/Forum/login.php on line 99

0

Del dette innlegget


Lenke til innlegg
Del på andre sider

Skrevet

header.php inneholder

<div id = "topcontent">

<img alt="php" src="images/header2.jpg">

</div>
Dette starter output og gjør at du ikke kan bruke header() etter dette i koden. Flytt include("header.php"); og løs problemet. Ser ikke noe grunn til å ha header før du er ferdig med all autentisering.
include("header.php");

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">

Viktig å huske på med header(); funksjonen er at du kan ikke ha noe som skriver til brukerens nettleser før den.

0

Del dette innlegget


Lenke til innlegg
Del på andre sider

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!


Start en konto

Logg inn

Har du allerede en konto? Logg inn her.


Logg inn nå

  • Hvem er aktive   0 medlemmer

    Ingen innloggede medlemmer aktive