The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Login PHP
C-j-123
post May 31 2007, 01:23 AM
Post #1





Group: Members
Posts: 9
Joined: 31-May 07
Member No.: 2,952



It gives me these errors when i try to log in using the right details...Other people get it too.
Warning: Cannot modify header information - headers already sent by (output started at /www/1111mb.com/u/g/n/ugnetwork/htdocs/index2.php:7) in /www/1111mb.com/u/g/n/ugnetwork/htdocs/index2.php on line 93

Warning: Cannot modify header information - headers already sent by (output started at /www/1111mb.com/u/g/n/ugnetwork/htdocs/index2.php:7) in /www/1111mb.com/u/g/n/ugnetwork/htdocs/index2.php on line 94

Warning: Cannot modify header information - headers already sent by (output started at /www/1111mb.com/u/g/n/ugnetwork/htdocs/index2.php:7) in /www/1111mb.com/u/g/n/ugnetwork/htdocs/index2.php on line 97

These are lines 87-104
CODE
// 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: index3.php");
}

}

} else {


And this is the whole code.

CODE
<html>
<title> </title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
<body bgcolor="#000000"><body>
<img src="Graphics/Title.png">
<center><hr>

  <?php

// Connects to your Database
mysql_connect("Hidden, "Hidden", "Hidden") or die(mysql_error());
mysql_select_db("ugnetwork_db") 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: index3.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('You did not fill in a required field.');
}

// checks it against the database

if (!get_magic_quotes_gpc()) {
  $_POST['username'] = addslashes($_POST['username']);
}

$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=Register.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: index3.php");
}

}

} else {

// if they are not logged in
?>
  <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
      <tr>
        <td colspan=2><img src="Graphics/Login.png"></td>
      </tr>
<tr><td><font color="#FFFFFF">Username:</font></td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td><font color="#FFFFFF">Password:</font></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
}


?>
<hr>
<a href="http://ugnetwork.1111mb.com/Register.php"><img src="Graphics/Register.png"></a>
</center>
</body>
</html>


Im Very New to php, so probally have made lots of mistakes.


*P.S* The code used in the sites changed, I just found this site and refound this code and figured this one is closer to working than the current code I have.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 31 2007, 04:14 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



Headers must be sent before any other content. From http://se.php.net/manual/en/function.header.php :

CODE
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
C-j-123
post May 31 2007, 11:36 PM
Post #3





Group: Members
Posts: 9
Joined: 31-May 07
Member No.: 2,952



So, would using it as; saving the php code to a new login.php document

Ok, I've changed the code to this
Login.html
CODE

<html>
<title> </title>
<script language="JavaScript" type="text/JavaScript">
*Hidden stuff*

<form action="login.php" method="POST">
Username: <input type="text" name="username" size="25" value=""><br>
Password: <input type="password" name="password" size="25"><br>
Remember me
<input type="checkbox" name="autologin"><br>
<input type="submit" value="Log In">
</form>







<hr>
  <a href="*******"><img src="Graphics/Register.png"></a>
<hr>
  <a href="*******"><img src="Graphics/Link To games.png"></a>
</center>
</body>
</html>


Login.php
CODE

<?php
include("usertrack.php");
if(isset($_GET["logout"])){
session_destroy();
setcookie("userhash", 0);
setcookie("uid", 0);
setcookie("auto", 0);
exit;
}
$error = 0;
if(!empty($_POST["username"]) && !empty($_POST["password"]) ){
!empty($_POST["autologin"]) ? $auto = 1 : $auto = 0;

$error = 0;
$password = $_POST["password"];
$result = mysql_query("select id, username, pass, active from users where username=\"{$_POST["username"]}\"");

$row = mysql_fetch_row($result);
$id = $row[0];
$username = $row[1];
$pass = $row[2];
$email = $row[3];
$active = $row[4];
//here we basically identify our variables
if($active == 1 && md5($password) == $pass){

$error = 0;
$_SESSION["logged_in"] = 1;
$_SESSION["uid"] = $id;
$_SESSION["userhash"] = $pass;
if($auto == 1){
setcookie("userhash", $pass, time()+60*60*24*30); //extend the cookies' life if the
setcookie("uid", $id, time()+60*60*24*30); //person wants to be remembered
} else {
setcookie("userhash", $pass);
setcookie("uid", $id); //temporary
}
//Set session variables, cookies, and what not.
if(isset($_SESION["redir"])) {
header("Location: http://ugnetwork.1111mb.com" . $_SESION["redir"]);
} else {
header("Location: http://ugnetwork.1111mb.com");
}
//This part is optinal, this basically sends the user back to where he was linked from or to.
} else {
$error = 1;
}


}
if($error == 1){
die("Something went wrong, try again");
}
?>


Usertrack.php
CODE
?php
session_start();
mysql_connect("localhost", "******", "******") or die("Unable to connect to server");
mysql_select_db('ugnetwork_db') or die("Unable to select database");
if(isset($_COOKIE["userhash"]) && isset($_COOKIE["uid"])){
if(isset($_COOKIE["uid"])){
$id = stripslashes($_COOKIE["uid"]);
} else {
$id = NULL;
}
if(isset($_COOKIE["passhash"])){
$pass = stripslashes($_COOKIE["passhash"]);
} else {
$pass = NULL;
}
$result = mysql_query("select username, pass, active from users where id=\"{$id}\"");
$row = mysql_fetch_row($result) or $loggedin = false;
//If nothing is returned or if there is an error, $loggedin = false;
$username = $row[0];
$pass = $row[1];
$check = $row[3];
if($pass == $_COOKIE["userhash"]){
$loggedin = true;
$_SESSION["uid"] = $_COOKIE["uid"];
//save user's id just in case
} else {
$loggedin = false;
}
} else {
$loggedin = false;
}
if(isset($check) && $check == 0) $loggein = false;
?>


Now, The errors dont show, but, once i enter the information, it takes me to a 404 page; wether the information is correct or not. yet, the place it should go exists... just won't go.

This post has been edited by C-j-123: May 31 2007, 11:40 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 1 2007, 09:50 AM
Post #4


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(C-j-123 @ Jun 1 2007, 06:36 AM) *

So, would using it as; saving the php code to a new login.php document

You might use a single login page like this (pseudo-code):

CODE
if(login==OK)
{
   header("Location: http://domain.com/index3.php"); // you must use a full URL for the redirect header
   exit;
}

else
{
   // Print HTML doument with login form here
}
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
C-j-123
post Jun 1 2007, 09:49 PM
Post #5





Group: Members
Posts: 9
Joined: 31-May 07
Member No.: 2,952



Ah!, Now I understand. Thank you for your help!.

This post has been edited by C-j-123: Jun 1 2007, 09:50 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 27th April 2024 - 12:55 PM