Page 1 of 1

strange authentication problem

Posted: Mon Jul 07, 2003 5:09 pm
by minds_gifts
hello,
I've written a user authentication script and i see it working quite strange.when i point to this location, http://localhost/project/index.php and enter the user name and password it does'nt work.
And when i point to this location, http://localhost/project/template.php, it redirects to index.php and when i enter the user name and password, its working
Please help me out with this one.
thank you

Heres the code for index.php

Code: Select all

<?php 

    session_start(); 
    ob_start(); 
    session_register(logged); 
    $_SESSION['logged'] = 0; 
?> 
<html> 
<head> 
    <title>Account Management Tool</title> 
</head> 

<body> 
<?php 
$redirect = $_GET['redirect']; 
    if (isset($_POST['submit'])) 
    { 
        if ($_POST['username'] == "admin" && $_POST['password'] == "phpbeginner")  
        { 
    $redirect = $_POST['redirect']; 
    $_SESSION['logged'] = 1; 
    header ("Location: $redirect"); 
        } 
    else 
    { 
?> 
    Invalid Username and/or Password<br><br> 
    <form action="index.php" method="post"> 
    <input type="hidden" name="redirect" value="<? echo $redirect; ?>"> 
    <table align="center" border="1" width="324" bgcolor="#CCCCCC" bordercolordark="black" bordercolorlight="black"> 
        <tr> 
            <td width="314" height="37"> 
                <p align="center"><span style="font-size:9pt;"><font face="Verdana">Please enter the user name and the password</font></span></p> 
                <p>&nbsp;&nbsp;<span style="font-size:11pt;">&nbsp;</span><font face="Verdana"><span style="font-size:11pt;">User 
                name </span>&nbsp;&nbsp;&nbsp;&nbsp;</font>&nbsp;<input type="text" name="username">&nbsp;</p> 
                <p>&nbsp;&nbsp;&nbsp;<span style="font-size:11pt;"><font face="Verdana">Password</font></span> 
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="password"></p> 
                <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Login" style="font-family:Verdana;"></p> 
                <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> 
            </td> 
        </tr> 
    </table> 
    </form> 
    <? 
    } 
} 
else 
{ 
?> 
You must be logged in to view this page<br><br> 
<form action="index.php" method="post"> 
<input type="hidden" name="redirect" value="<? echo $redirect; ?>"> 
<table align="center" border="1" width="324" bgcolor="#CCCCCC" bordercolordark="black" bordercolorlight="black"> 
        <tr> 
            <td width="314" height="37"> 
                <p align="center"><span style="font-size:9pt;"><font face="Verdana">Please enter the user name and the password</font></span></p> 
                <p>&nbsp;&nbsp;<span style="font-size:11pt;">&nbsp;</span><font face="Verdana"><span style="font-size:11pt;">User 
                name </span>&nbsp;&nbsp;&nbsp;&nbsp;</font>&nbsp;<input type="text" name="username">&nbsp;</p> 
                <p>&nbsp;&nbsp;&nbsp;<span style="font-size:11pt;"><font face="Verdana">Password</font></span> 
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="password"></p> 
                <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Login" style="font-family:Verdana;"></p> 
                <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> 
            </td> 
        </tr> 
    </table> 
</form> 
<? 
} 
?> 
</body> 
</html>

Posted: Mon Jul 07, 2003 5:25 pm
by cactus
You seem to be starting ouput buffering, ob_start(), but don't seem to end it nor use its contents in the above script.

Have a look at:

Ref : LXXV. Output Control Functions
http://uk.php.net/manual/en/ref.outcontrol.php

Also, what is the contents of the other file "template.php" ?

Hope this helps.

Regards,

Posted: Mon Jul 07, 2003 5:47 pm
by minds_gifts
hello cactus,
Heres the code for the other files:
I would be glad if you can help me.
Thanks
template.php

Code: Select all

<?
include 'auth.inc.php';
?>
<html>
<head>
	<title>Untitled</title>
</head>
<body>
this is template.php
</body>
</html>
and code for auth.inc.php

Code: Select all

<?
session_start();
if ($_SESSION['logged'] != 1)
{
$redirect = $_SERVER['PHP_SELF'];
header("Location: index.php?redirect=$redirect");
}
?>

Posted: Mon Jul 07, 2003 6:35 pm
by cactus
It does seem like you are using ob_start() but for no reason (ie: you are not ending the output buffering or assigning its contents to a var), comment out the ob_start() line in index.php and give it another try.

Note, you will probably get an error using header() after you have sent HTML output, see:

viewtopic.php?t=1157

Regards,

Posted: Mon Jul 07, 2003 6:43 pm
by Galahad
Read the third caution on the session_register manual page. That may or may not solve your problems, but seems like a decent place to start. Try getting rid of the session_register type stuff and just using something like $_SESSION['var'] = value;