Authenticating a username and password - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Authenticating a username and password

ITAITA Posts: 463Member
I've just learnt the basics of PHP and am now moving on to connecting to MySQL databases and the like. What I am going to do is to validate a username and pass against an entry in a database. However I can't even get the login box and headers to show up on the page. I simply get a parse error. I am using Linux and Apache as a server + PHP4. (LAMP).Please can anyone help? Many thanks.

ITA

These are the error messages which I recieve:
Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/php/members_only.php:11) in /srv/www/htdocs/php/members_only.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/php/members_only.php:11) in /srv/www/htdocs/php/members_only.php on line 19


Here is my code (it is simplified - it leaves out DB connections and simply tries to get the user to login).

[code]


This is a simple page which authenticates a password and username







<?php
if($_POST['action'] == 'submitted')
{
$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
if(!isset($user))
{
header('HTTP/1.0 401 Unauthorized');
header('WWW-Authenticate: Basic realm="My Realm"');
echo 'You are not logged in';
exit;
}
else
{
echo "<p>Hello $user.";
echo "

You entered $pass as your password.

";
}
}
?>



[/code]

Comments

  • JonathanJonathan Posts: 2,914Member
    : I've just learnt the basics of PHP and am now moving on to connecting to MySQL databases and the like. What I am going to do is to validate a username and pass against an entry in a database. However I can't even get the login box and headers to show up on the page. I simply get a parse error. I am using Linux and Apache as a server + PHP4. (LAMP).Please can anyone help? Many thanks.
    :
    : ITA
    :
    : These are the error messages which I recieve:
    : Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/php/members_only.php:11) in /srv/www/htdocs/php/members_only.php on line 18
    :
    : Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/php/members_only.php:11) in /srv/www/htdocs/php/members_only.php on line 19
    :
    :
    : Here is my code (it is simplified - it leaves out DB connections and simply tries to get the user to login).
    :
    : [code]
    :
    :
    : This is a simple page which authenticates a password and username
    :
    :
    :
    :
    :
    :
    :
    : <?php
    : if($_POST['action'] == 'submitted')
    : {
    : $user = $_SERVER['PHP_AUTH_USER'];
    : $pass = $_SERVER['PHP_AUTH_PW'];
    : if(!isset($user))
    : {
    : header('HTTP/1.0 401 Unauthorized');
    : header('WWW-Authenticate: Basic realm="My Realm"');
    : echo 'You are not logged in';
    : exit;
    : }
    : else
    : {
    : echo "<p>Hello $user.";
    : echo "

    You entered $pass as your password.

    ";
    : }
    : }
    : ?>
    :
    :
    :
    : [/code]
    :
    You must not send any HTML etc before you send the www-authenticate headers. So basically you need to shift your code around a bit.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • DarQDarQ Posts: 1,625Member
    quite simple actually:

    OUTPUT before HTTP_HEADERS

    what does this mean?? that you cannot have ANY HTML _before_ sending a http_header. well, i see you have html _before_ sending a http_header. you send that header with, hey, the function header!

    you must not have ANY html, php-generated hmtl before sending http_headers with header, set_cookie, session_start() (any maybe some more).

    its actually not very nice to have php code between html. if you don't want to seperate code and html, then at least have actions such as logging in, reading from a database etc before html.

    put you php code above all html

    : I've just learnt the basics of PHP and am now moving on to connecting to MySQL databases and the like. What I am going to do is to validate a username and pass against an entry in a database. However I can't even get the login box and headers to show up on the page. I simply get a parse error. I am using Linux and Apache as a server + PHP4. (LAMP).Please can anyone help? Many thanks.
    :
    : ITA
    :
    : These are the error messages which I recieve:
    : Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/php/members_only.php:11) in /srv/www/htdocs/php/members_only.php on line 18
    :
    : Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/php/members_only.php:11) in /srv/www/htdocs/php/members_only.php on line 19
    :
    :
    : Here is my code (it is simplified - it leaves out DB connections and simply tries to get the user to login).
    :
    : [code]
    :
    :
    : This is a simple page which authenticates a password and username
    :
    :
    :
    :
    :
    :
    :
    : <?php
    : if($_POST['action'] == 'submitted')
    : {
    : $user = $_SERVER['PHP_AUTH_USER'];
    : $pass = $_SERVER['PHP_AUTH_PW'];
    : if(!isset($user))
    : {
    : header('HTTP/1.0 401 Unauthorized');
    : header('WWW-Authenticate: Basic realm="My Realm"');
    : echo 'You are not logged in';
    : exit;
    : }
    : else
    : {
    : echo "<p>Hello $user.";
    : echo "

    You entered $pass as your password.

    ";
    : }
    : }
    : ?>
    :
    :
    :
    : [/code]
    :

    [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]
    Jou my no rap dy lekkere dikke tsjap

  • ITAITA Posts: 463Member
    Thanks a million - I was always told one of the greatest things about PHP was that you could embed it into HTML (courtesy of the PHP manual). Thanks for telling me, I did't actually know that you could put the PHP code into separate files! - I think the PHP man should be updated!

    ITA
  • ITAITA Posts: 463Member
    I did as you suggested and re-arranged my code. I kept the PHP in the same file but simply put it before the HTML. I still get the same warning messages though except with different line numbers (The PHP has been moved). I've even tried putting the PHP in separate files so that the HTML page submits the form to a PHP script but to no avail. I've also tried having no HTML and just running the script on its own. All has failed. Have you any other suggestions? If you have time please could you give an example of how to accomplish the task - rearrange my code - I may have done it wrong.

    Please help. Many thanks.

    ITA



  • DarQDarQ Posts: 1,625Member
    seperating php and html is quite a trick without using classes i guess. anyway, such a technique is beyond the scope of the php manual.

    i think you should not seperate the code in different files. but try to keep logic above, and formatting below (mixed with html)

    anyway, if you have a problem, then at least copy/paste the error message and the code.

    : I did as you suggested and re-arranged my code. I kept the PHP in the same file but simply put it before the HTML. I still get the same warning messages though except with different line numbers (The PHP has been moved). I've even tried putting the PHP in separate files so that the HTML page submits the form to a PHP script but to no avail. I've also tried having no HTML and just running the script on its own. All has failed. Have you any other suggestions? If you have time please could you give an example of how to accomplish the task - rearrange my code - I may have done it wrong.
    :
    : Please help. Many thanks.
    :
    : ITA
    :
    :
    :
    :



    [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]
    Jou my no rap dy lekkere dikke tsjap

Sign In or Register to comment.