tags:

views:

47

answers:

3

I am trying to use a cookie with authentication.

This page works once entering user and pass

   <?
    if ((!$_POST[username]) || (!$_POST[password])) {
        header("Location: show_login.html");
        exit;
    }
    $db_name = "testDB";
    $table_name = "auth_users";
    $connection = @mysql_connect("localhost", "user", "pass") or die(mysql_error());
    $db = @mysql_select_db($db_name, $connection) or die(mysql_error());
    $sql = "SELECT * FROM $table_name WHERE username ='$_POST[username]' AND password = password('$_POST[password]')";
    $result = @mysql_query($sql, $connection) or die(mysql_error());
    $num = mysql_num_rows($result);
        if ($num != 0) {
            $cookie_name = "auth";
            $cookie_value = "ok";
            $cookie_expire = "0";
            $cookie_domain = "domain.com.au";
            setcookie($cookie_name, $cookis_value, $cookie_expire, "/", $cookie_domain, 0);
            $display_block = "
            <p><strong>Secret Menu:</strong></p>
            <ul>
                <li><a href=\"secretA.php\">secret page A</a>
                <li><a href=\"secretB.php\">secret page B</a>
            </ul>"; 
        } else {
            header("Location: show_login.html");
            exit;
        }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Secret Area</title>
    </head>

    <body>
    <? echo "$display_block"; ?>


    </body>
    </html>

WHen clicking on either secretA.php or secretB.php I am redirected to log in again, it should work. here is the code. secretB.php

<?php

if ($_COOKIE[auth] == "ok") {
    $msg = "<p>Welcome to secret page B, authorised user! </P>";
} else {
    header( "Location: /show_login.html");
    exit;
}
?>
<HTML>
<HEAD>
<title>Secret Page B:</title>
</HEAD>
<BODY>

<? echo "$msg"; ?>

</BODY>
</HTML>
+1  A: 

This if the great example of bad code.
Thanks for posting it. Many people can read it and learn from this.

Let me explain.
PHP can help you to find some obscure errors. Not every one but some of them.
For example, if you mistyped a variable name, PHP will throw an error... of course if you let PHP to say.
To make every error visible, error reporting level must be maxed. To do it, every script must contain this line:

error_reporting(E_ALL);

after you add it, if it was properly written code, you would see only one error message pointing to the mistyped variable. But.
Instead you will see a waterfall of errors. Because PHP cannot distinguish intentional errors from occasional ones.

Thus. It must be no intentional errors in the code, to let you see occasional ones.
This is the great lesson worth to remember.

What are these errors?

  1. Strings in php being delimited by quotes.
    So, if you have a string username, it must be written as "username".
    And $_POST[username] become $_POST["username"].
    (and contrary, variables do not need quotes, so, echo "$msg"; must be echo $msg;)
  2. all variables must be set or checked for existence. so,
    if ((!$_POST[username]) || (!$_POST[password])) {
    must become
    if (!empty($_POST["username"]) OR !empty($_POST["password"])) {

With such a code you will see only occasional error and be able to correct it immediately.

Col. Shrapnel
Thanks for takign the time to post that info. will be a great help in the long run. cheers
Jacksta
+1  A: 

Sometimes its the simplest solution. The same thing happened to me. I was running on localhost (wampserver). I found out that my firewall was blocking all cookie requests from localhost. I used ZoneAlarm at the time but I assume other firewalls could have the same effect. Try disabling your firewall to see if it still doesnt work.

Ozzy
Hey thanks for the tip, I am actually runnign this on a live server.
Jacksta
a programmer always must go not for the simplest solution but for the debugging
Col. Shrapnel
+1  A: 
try this :
   <?php
    if (isset($_POST['username']) and isset($_POST['password'])) 
    {

    $db_name = "testDB";
    $table_name = "auth_users";
    $connection = @mysql_connect("localhost", "user", "pass") or die(mysql_error());
    $db = @mysql_select_db($db_name, $connection) or die(mysql_error());
    $sql = "SELECT * FROM $table_name WHERE username ='".$_POST['username']."' AND password = password('".$_POST['password']."')";
    $result = @mysql_query($sql, $connection) or die(mysql_error());
    $num = mysql_num_rows($result);
        if ($num != 0) {
            $cookie_name = "auth";
            $cookie_value = "ok";
            $cookie_expire = "0";
            $cookie_domain = "domain.com.au";
            setcookie($cookie_name, $cookis_value, $cookie_expire, "/", $cookie_domain, 0);
            $display_block = "
            <p><strong>Secret Menu:</strong></p>
            <ul>
                <li><a href=\"secretA.php\">secret page A</a>
                <li><a href=\"secretB.php\">secret page B</a>
            </ul>"; 
        } else {
            header("Location: show_login.html");
            exit;
        };


    }
    else
    {
    header("Location: show_login.html");
    exit;
    };
cosy
still the sajme outcome = log in again! thanks for the suggestion
Jacksta
still the same error lol
Col. Shrapnel