Gå til innhold

Logg inn side. Hva gjør jeg feil?


Anbefalte innlegg

Hei.

 

Jeg skal lage en logg inn side i php, men den funker ikke. Noen som kan hjelpe?

 

 

Her er index.php

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php 

// Connects to your Database 

mysql_connect("localhost", "root", "") or die(mysql_error()); 

mysql_select_db("brukere") or die(mysql_error()); 


//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: members.php");



			}

		}

}


//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('Du fylte ikke et eller flere felt!');

	}

	// 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('Brukeren finnes ikke!');

				}

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('Feil passord. Prøv igjen.');

	}
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"); 

} 

} 

} 

else 

{	 



// if they are not logged in 

?> 
<!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>Logg inn</title>

<link href="login-box.css" rel="stylesheet" type="text/css" />
</head>

<body>


<div style="padding: 100px 0 0 250px;">


<div id="login-box">

<H2>Logg innn</H2>
Her kan du logge inn
<br />
<br />
<div id="login-box-name" style="margin-top:20px;">Brukernavn</div><div id="username" style="margin-top:20px;"><input name="q" class="form-login" title="username" value="" size="30" maxlength="2048" /></div>
<div id="login-box-name">Passord:</div><div id="pass"><input name="q" type="password" class="form-login" title="pass" value="" size="30" maxlength="2048" /></div>
<br />
<br />
<br />
<a href="#"><img src="images/login-btn.png" width="103" height="42" style="margin-left:90px;" /></a>


</div>
</div>
</body>
</html>

<?php 

} 



?> 

 

 

 

 

Her er login-box.css

 

 

#login-box {
width:333px;
height: 352px;
padding: 58px 76px 0 76px;
color: #ebebeb;
font: 12px Arial, Helvetica, sans-serif;
background: url(images/login-box-backg.png) no-repeat left top;
}

#login-box img {
border:none;
}

#login-box h2 {
padding:0;
margin:0;
color: #ebebeb;
font: bold 44px "Calibri", Arial;
}


#login-box-name {
float: left;
display:inline;
width:80px;
text-align: right;
padding: 14px 10px 0 0;
margin:0 0 7px 0;
}

#login-box-field {
float: left;
display:inline;
width:230px;
margin:0;
margin:0 0 7px 0;
}


.form-login  {
width: 205px;
padding: 10px 4px 6px 3px;
border: 1px solid #0d2c52;
background-color:#1e4f8a;
font-size: 16px;
color: #ebebeb;
}


.login-box-options  {
clear:both;
padding-left:87px;
font-size: 11px;
}

.login-box-options a {
color: #ebebeb;
font-size: 11px;
}

 

 

Lenke til kommentar
Videoannonse
Annonse

Mulig jeg overser noe, men mangler du ikke <form> </form> tags?

Trodde det var nødvendig for å hente ut $_POST informasjon.

 

<H2>Logg innn</H2>
Her kan du logge inn
<br />
<br />
<form name="login" action="dittlogginnscript.php" method="post">
<div id="login-box-name" style="margin-top:20px;">Brukernavn</div><div id="username" style="margin-top:20px;"><input name="q" class="form-login" title="username" value="" size="30" maxlength="2048" /></div>
<div id="login-box-name">Passord:</div><div id="pass"><input name="q" type="password" class="form-login" title="pass" value="" size="30" maxlength="2048" /></div></form>

Lenke til kommentar

Mulig jeg overser noe, men mangler du ikke <form> </form> tags?

Trodde det var nødvendig for å hente ut $_POST informasjon.

 

<H2>Logg innn</H2>
Her kan du logge inn
<br />
<br />
<form name="login" action="dittlogginnscript.php" method="post">
<div id="login-box-name" style="margin-top:20px;">Brukernavn</div><div id="username" style="margin-top:20px;"><input name="q" class="form-login" title="username" value="" size="30" maxlength="2048" /></div>
<div id="login-box-name">Passord:</div><div id="pass"><input name="q" type="password" class="form-login" title="pass" value="" size="30" maxlength="2048" /></div></form>

 

Klarer forsatt ikke logge inn :hmm:

 

Får bare /# på slutten av url'en.

Lenke til kommentar

Det er på grunn av den kodelinjen ja. Om du skriver

<input type="submit" value="Submit" />

så vil det (forhåpentligvis) funke, men du kan også skrive

<input type="image" src="images/login-btn.png" width="103" height="42" style="margin-left:90px;" />

 

Det bør gjøre bildet til submit-knapp, ref denne guiden.

Lenke til kommentar




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php 

// Connects to your Database 

mysql_connect("localhost", "root", "") or die(mysql_error()); 

mysql_select_db("brukere") or die(mysql_error()); 


//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: members.php");



                       }

               }

}


//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('Du fylte ikke et eller flere felt!');

       }

       // 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('Brukeren finnes ikke!');

                               }

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('Feil passord. Prøv igjen.');

       }
       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"); 

} 

} 

} 

else 

{        



// if they are not logged in 

?> 
<!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>Logg inn</title>

<link href="login-box.css" rel="stylesheet" type="text/css" />
</head>

<body>


<div style="padding: 100px 0 0 250px;">


<div id="login-box">

<H2>Logg innn</H2>
Her kan du logge inn
<br />
<br />
<form name="login" action="dittlogginnscript.php" method="post">
<div id="login-box-name" style="margin-top:20px;">Brukernavn</div>
<div id="username" style="margin-top:20px;">
<input name="q" class="form-login" title="username" value="" size="30" maxlength="2048" />
</div><div id="login-box-name">Passord:</div><div id="pass"><input name="q" type="password" class="form-login" title="pass" value="" size="30" maxlength="2048" /></div>
<br />
<input type="image" src="images/login-btn.png" width="103" height="42" style="margin-left:90px;" />
</form>
</div>
</div>
</body>
</html>

<?php 

} 



?> 


Lenke til kommentar

 




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php 

// Connects to your Database 

mysql_connect("localhost", "root", "") or die(mysql_error()); 

mysql_select_db("brukere") or die(mysql_error()); 


//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: members.php");



                       }

               }

}


//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('Du fylte ikke et eller flere felt!');

       }

       // 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('Brukeren finnes ikke!');

                               }

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('Feil passord. Prøv igjen.');

       }
       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"); 

} 

} 

} 

else 

{        



// if they are not logged in 

?> 
<!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>Logg inn</title>

<link href="login-box.css" rel="stylesheet" type="text/css" />
</head>

<body>


<div style="padding: 100px 0 0 250px;">


<div id="login-box">

<H2>Logg innn</H2>
Her kan du logge inn
<br />
<br />
<form name="login" action="dittlogginnscript.php" method="post">
<div id="login-box-name" style="margin-top:20px;">Brukernavn</div>
<div id="username" style="margin-top:20px;">
<input name="q" class="form-login" title="username" value="" size="30" maxlength="2048" />
</div><div id="login-box-name">Passord:</div><div id="pass"><input name="q" type="password" class="form-login" title="pass" value="" size="30" maxlength="2048" /></div>
<br />
<input type="image" src="images/login-btn.png" width="103" height="42" style="margin-left:90px;" />
</form>
</div>
</div>
</body>
</html>

<?php 

} 



?> 


 

 

Det funker, men om man skriver feil passord så kommer man til samme side som om man skriver rett passord :hmm:

Lenke til kommentar

Åpenbart. Du må sjekke på den siden man redirectes til hvorvidt passordet var rett eller ei. Du bruker jo cookies.

 

Forresten, du har en potensiell sikkerhetssvakhet der. Scriptet genererer forskjellige feilmeldinger avhengig av om det er bruker eller passord som er feil, på den måten kan en inntrenger finne ut om et gitt brukernavn er gyldig eller ikke. Du bør ha disse unntakene i samme if-blokk og ha en mer generell feilmelding ("brukernavn eller passord er feil").

Endret av srbz
Lenke til kommentar

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å
×
×
  • Opprett ny...