PDA

View Full Version : log in, log out help


Karkar
06-11-2007, 07:48 AM
My script wont launch. I have a feeling it has something to do with the way I wrote this:

session_start();
if (! isset($_SESSION['id'])){
header("location: login.php");
exit;
}
$id = $_SESSION['id'];

Is that right??

davidj
06-11-2007, 09:06 AM
is this the security.php

Karkar
06-11-2007, 09:10 AM
not hats welcome.php

Karkar
06-11-2007, 09:14 AM
Welcoem.php



<?php
require_once("connections/connection.php"); // database connection
session_start();
if (! isset($_SESSION['id'])){
header("location: login.php");
exit;
}
$id = $_SESSION['id'];
/////////////////////
$query = sprintf("SELECT * FROM users where user_id='$id' ");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////
?>
<!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>Untitled Document</title>
</head>
<body>
Welcome to the application, <?php echo $rowAccount['user_name'];?>
</body>
</html>

login.php


<?php
require_once("connections/connection.php"); // database connection
session_start();
$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];
if ($userid && $password) {
/////////////////////
$query = sprintf("SELECT * FROM users where user_name='$userid' and user_password = '$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////
}
if ($rowAccount) {
$_SESSION['id'] = $rowAccount['user_id'];
header("location: welcome.php");
exit;
}elseif ($submitted){
echo "Go away !!";
}

?>

<!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>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="309" height="211" border="1" align="center">
<tr>
<td width="106">User ID </td>
<td width="143"><label>
<input name="userid" type="text" id="userid" />
</label></td>
</tr>
<tr>
<td>Password</td>
<td><label>
<input name="password" type="text" id="password" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submitted" type="hidden" id="submitted" value="1" />
<label>
<input type="submit" name="Submit" value="Submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>

davidj
06-11-2007, 09:21 AM
whats happening K

is it not loggin in

Karkar
06-11-2007, 11:22 AM
it shows the form with these error messages.

Warning: session_start() [function.session-start (http://localhost/test/function.session-start)]: Cannot send session cookie - headers already sent by (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\login.php on line 4

Warning: session_start() [function.session-start (http://localhost/test/function.session-start)]: Cannot send session cache limiter - headers already sent (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\login.php on line 4

When I attempt to login it shows these error messages:

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\login.php on line 22

davidj
06-11-2007, 11:31 AM
where you have this...

echo "Go away !!";

assign this to a variable

dont echo anything above html

try this then run it again

Karkar
06-11-2007, 01:33 PM
i dont quite understand what you mean. why would i assign this to a variable. I dont beleive this is what you did inthe tutorial?

davidj
06-11-2007, 01:44 PM
yes it is what i did. The tutorial were the basics. Now its time to progress up a level

The error your getting is reference to header output. if some text is echo'd befor the HTML is sent then you could get an error plus you want to have total control over your code and what echo's where. By assigning the message to a var like this...
$warning = "Go away !!";
it means you can echo $warning anywhere on the screen you want rather than having it display above everything like your container or banner

does this makes sense

Karkar
06-11-2007, 02:00 PM
this does not seem to solve the header problem though.

If assigning this avariable like this :

elseif ($submitted){
echo $warning = "Go away !!";

}
would i then need to add it here:

session_start();
$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];
...........

davidj
06-11-2007, 02:14 PM
you have mis understood

where you have this...
elseif ($submitted){
echo $warning = "Go away !!";

}

delete the echo. You just want to set the contents of the variable here not display it


elseif ($submitted){
$warning = "Go away !!";
}


then in the html just echo the message anywhere you like inside the <body.. tags
<body>
<?php echo $warning; ?>
</body>

Karkar
06-11-2007, 02:27 PM
ohhhhhhhhhhhhhhhhhhhh ok sorrry sorry

Karkar
06-11-2007, 02:29 PM
Ok that's working fine now. But the header problem is still confusiing me


<?php
require_once("connections/connection.php"); // database connection
session_start();
$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if ($userid && $password) {
/////////////////////
$query = sprintf("SELECT * FROM users where user_name='$userid' and user_password = '$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////
}
if ($rowAccount) {
$_SESSION['id'] = $rowAccount['user_id'];
header("location:welcome.php");
exit;
}elseif ($submitted){
$warning = "Go away !!";

}

?>

<!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>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="309" height="211" border="1" align="center">
<tr>
<td width="106">User ID </td>
<td width="143"><label>
<input name="userid" type="text" id="userid" />
</label></td>
</tr>
<tr>
<td>Password</td>
<td><label>
<input name="password" type="text" id="password" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submitted" type="hidden" id="submitted" value="1" />
<label>
<input type="submit" name="Submit" value="Submit" />
</label></td>
</tr>
</table>
</form>
<p><?php echo $warning ?></p>
</body>
</html>

davidj
06-11-2007, 02:33 PM
ok so your still having problems with your script

whats happening ?

Karkar
06-11-2007, 02:44 PM
Warning: session_start() [function.session-start (http://localhost/test/function.session-start)]: Cannot send session cookie - headers already sent by (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\login.php on line 4

Warning: session_start() [function.session-start (http://localhost/test/function.session-start)]: Cannot send session cache limiter - headers already sent (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\login.php on line 4

are the errors i get

Is this tell me that connection.php is already doing something that i'm asking login.php to do?

davidj
06-11-2007, 02:46 PM
are you echo-ing any code in your connection.php

Karkar
06-11-2007, 02:47 PM
Nope. here is the code

<?php
// this is a database connection script //
$database = "dwc";
$username = "dj";
$password = "hello";
$link = @mysql_connect('localhost', $username, $password);
$db = mysql_select_db($database, $link);
?>

davidj
06-11-2007, 02:50 PM
is there any whitespace before the <?php ...in the connections.php


//////// space here //////////
//////// space here //////////
<?php
// this is a database connection script //
$database = "dwc";
$username = "dj";
$password = "hello";
$link = @mysql_connect('localhost', $username, $password);
$db = mysql_select_db($database, $link);
?>

Karkar
06-11-2007, 02:53 PM
nope. none.

If I try launching welcome.php i get these errors:

Warning: session_start() [function.session-start (http://localhost/test/function.session-start)]: Cannot send session cookie - headers already sent by (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\welcome.php on line 4

Warning: session_start() [function.session-start (http://localhost/test/function.session-start)]: Cannot send session cache limiter - headers already sent (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\welcome.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\welcome.php on line 8

I dont know what's supposed to happen when launching welcome.php properly. I assume nothing because of this:

if (! isset($_SESSION['id'])){
header("location:login.php");
exit;
}


??

davidj
06-11-2007, 02:58 PM
try this...

move the session_start() above the require_once call like this...

session_start();
require_once("connections/connection.php"); // database connection


Make sure there are no blank lines above the <?php on any pages also

Karkar
06-11-2007, 03:03 PM
Ok i did this and the the login page appears with no errors. I then entered in the correct details and submitted. WhenI did this I got this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\Alex\My Documents\web root\test\connections\connection.php:12) in C:\Documents and Settings\Alex\My Documents\web root\test\login.php on line 24

So I then moved the session_start() above the require_once call on the welcome.php page aswell.

Nothing changed. The only thing that changed is that I can now launch the welcome.php from welcome.php...

Karkar
06-11-2007, 03:05 PM
Oh its ok i fixed it some how

Karkar
06-11-2007, 03:06 PM
i ahve no idea what i did. I think i just saved connections.php... even though i dont think i changed naything

Karkar
06-11-2007, 03:09 PM
How is it that it worked for you

require_once call above the session_start() ??