Link to home
Start Free TrialLog in
Avatar of cleany
cleany

asked on

basic Auth class loginfunction

Alright...this almost must be a simple question.

I used the original Auth example from the PEAR class manual

<?
require_once "Auth/Auth.php";

function loginFunction()
{

    echo "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "?login=1\">";
    echo "<input type=\"text\" name=\"username\">";
    echo "<input type=\"password\" name=\"password\">";
    echo "<input type=\"submit\">";
    echo "</form>";
}

if ($_GET['login'] == 1) {
    $optional = true;
} else {
    $optional = false;
}

$dsn = "mysql://user:password@localhost/database";

$a = new Auth("DB", $dsn, "loginFunction", $optional);

$a->start();

echo "Everybody can see this text!<br />";

if (!isset($_GET['login'])) {
    echo "<a href=\"test.php?login=1\">Click here to log in</a>\n";
}

if ($a->getAuth()) {
    echo "One can only see this if he is logged in!";
}
?>

but I get an error: Fatal error: Undefined class name 'db' in /opt/guide/www.tijdelijk.nl/HTML/Auth/Container/DB.php on line 130

could someone tell me why this is or give me an example of an Auth class.
I want to make a loginfunction which checks on IP, sends cookies and makes a sessioncheck.
If someone can provide me with enough information to make something like that, he or she will receive 500 points !!!
Avatar of cleany
cleany

ASKER

I`ve got a database with a table:

people with fields "personid", "personname", "email", "password" and "persontype"
ASKER CERTIFIED SOLUTION
Avatar of php-webdesign
php-webdesign

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cleany

ASKER

I hope so...dank je...

eigenlijk moest ik ook iets met PEAR classes doen....
dat weet je zeker niet ?


if it works the 500 points are for you, but I really need to have PEAR classes working along with this...
well i am sure this script works... but it isn't build with the PEAR classes sorry

never really worked with the pear class... but you can anyway use the script
Avatar of cleany

ASKER

hmm...echt vreemd...

"Foute login gegevens"

krijg ik als melding...net als bij die andere...
Avatar of cleany

ASKER

alright...what does it say if you go to http://www.addrenaline.com/~ip-03/login

and inlognaam->test and wachtwoor->test
indeed strange... try putting error_reporting(E_ALL); on
Avatar of cleany

ASKER

Parse error: parse error in /home/ip-03/public_html/login/login.php on line 4

huh ?

this is line 4: session_start();
Avatar of cleany

ASKER

database:

CREATE TABLE `people` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(6) NOT NULL default '',
  `password` varchar(10) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;
really strange.....

don't know what that could be
Avatar of cleany

ASKER

ja het is niet echt de eerste keer dat zoiets me overkomt...echt gestoord word je er van...

hoe ziet je tabel eruit dan ?
CREATE TABLE `beheer` (
`inlognaam` varchar(50) NOT NULL default 'gebruiker',
`wachtwoord` varchar(50) NOT NULL default 'wachtwoord',
UNIQUE KEY `inlognaam` (`inlognaam`)
) TYPE=MyISAM;
Avatar of cleany

ASKER

geen id ?
oooow sorry

CREATE TABLE `people` (
`id` int(11) NOT NULL auto_increment,
`inlognaam` varchar(50) NOT NULL default 'gebruiker',
`wachtwoord` varchar(50) NOT NULL default 'wachtwoord',
UNIQUE KEY `inlognaam` (`inlognaam`)
) TYPE=MyISAM;
Avatar of cleany

ASKER

probably i`m doing something wrong, but I don`t know what...

this loginscript works:

<br><form method="post" action="login.php">
<table width="50%" border="5" bgcolor="#F0F7B7" bordercolor="#BBF0D4" cellpadding="0" cellspacing="0">
<tr>
<td>voer hieronder je naam en wachtwoord in:</td>
</tr>
<tr>
<td>Naam
<input type="hidden" name="Submit" value="1"></td>
<td> <input type="text" name="naam"> </td>
</tr>
<tr>
<td>Wachtwoord</td>
<td> <input type="password" name="passwd"> </td>
</tr>
<tr>
<td>&nbsp;</td>
<td> <input name="submit" type="submit" value="Login">
</td>
</tr>
<p>
<?php

If ($Submit) {
$db = mysql_connect ($dbhost, $dbname, $dbpasswd) or die ("Verbinding mislukt");
mysql_select_db($dbname, $db);
$naam = addslashes($naam);
$passwd = addslashes($passwd);
$query = "SELECT user_id, naam, passwd FROM users WHERE naam = '$naam' AND passwd = '$passwd' ORDER BY user_id ASC";
$Recordset1      = mysql_query($query);
$id = mysql_fetch_assoc($Recordset1);
$Results = mysql_num_rows($Recordset1);

if ($Results == '1') {
$Login = "1";
$Idcode = $id['user_id'];
$Nickname = $id['naam'];

session_register("Login");
session_register("Idcode");
session_register("naam");

echo "<p><tr><td>welkom <b>";
echo $Nickname;
echo "</b></td></tr><tr><td>Je bent ingelogd. Klik <a href=\"index.php\" target=\"_blank\">hier</a> om verder te gaan.</td></tr>";
echo "<tr><td>Om je wachtwoord te wijzigen ga naar <a href=\"passwd.php\">wachtwoord</a></td></tr>";
echo "</p>";
}
else {
echo "<p><BR>Je gebruikersnaam of password klopt niet.</p>";
}
}
?>
</p>
</form>

but the one above doesn`t....strange...
Avatar of cleany

ASKER

I mean the other way around...
can't follow you :s
Avatar of cleany

ASKER

alright...this above is also a loginscript (not save though), but this works, so I don`t understand why this one works and the other one doesn`t...
is your save_session_path etc set in your php.ini?

Avatar of cleany

ASKER

session.save_path = /tmp

maar dit is een remote adres, of maakt dat niet uit ?
Avatar of cleany

ASKER

there`s no remote php.ini in the sites dir`s

but the script above also uses session_start();
session variables are stored on the SERVER!!

so you need to apply that path to an existing path. an CHMOD it 777
Avatar of cleany

ASKER

Now it says...

Warning: Cannot modify header information - headers already sent by (output started at /home/ip-03/public_html/login/includes/connect.php:15) in /home/ip-03/public_html/login/login.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at /home/ip-03/public_html/login/includes/connect.php:15) in /home/ip-03/public_html/login/login.php on line 35

it gets weirder all the time....
session_start(); needs to be at the very top of the page!
Avatar of cleany

ASKER

it is...
session_register("Login");
session_register("Idcode");
session_register("naam");

replace with

$_SESSION['login'] = $Login;

etc
Avatar of cleany

ASKER

alright but I`m using yours right now, because I thought that maybe the chmod was the problem...
that is really really really strange :s

works fine for me
Avatar of cleany

ASKER

ok...what is this !?

Notice: Undefined index: wachtwoord in /opt/guide/www.tijdelijk.nl/HTML/test/login.php on line 13

Notice: Undefined index: inlognaam in /opt/guide/www.tijdelijk.nl/HTML/test/login.php on line 17
Foute login gegevens
that is a notice... if you now put error_reporting(E_ALL); off by commenting the line... i think it will work
Avatar of cleany

ASKER

nee...het is hopeloos...
sorry, dan weet ik het ook niet meer
Avatar of cleany

ASKER

hij zegt gewoon dat de combinatie gebruiker/password niet klopt terwijl dat wel zo is...
heb je het wachtwoord er wel als MD5 in de database staan?!
Avatar of cleany

ASKER

hij staat er gehashed...
dan snap ik het niet :(
je kan kijken als je de waarde uit de database echoed....

en zelf met je eigen oog deze waarde vergelijken...
Avatar of cleany

ASKER

ik krijg deze meldingen....

Notice: Undefined variable: inlognaam in /opt/guide/www.tijdelijk.nl/HTML/login.php on line 25

Warning: Cannot modify header information - headers already sent by (output started at /opt/guide/www.tijdelijk.nl/HTML/inc_connect.php:11) in /opt/guide/www.tijdelijk.nl/HTML/login.php on line 31

Warning: Cannot modify header information - headers already sent by (output started at /opt/guide/www.tijdelijk.nl/HTML/inc_connect.php:11) in /opt/guide/www.tijdelijk.nl/HTML/login.php on line 34

kan je deze pagina zien?:
http://212.204.230.90/login.php

login.php=
<?php
error_reporting(E_ALL);
//start de sessie
session_start();

//als de pagina goed is aangeroepen gaan we verder
if(!empty($_POST)){

//include de databse connectie
    include("inc_connect.php");

//encrypt het net verstuurde wachtwoord met md5
    $wachtwoord = md5($_POST["wachtwoord"]);

//zet de query in een variabele
    $query = "SELECT * FROM beheer
        WHERE inlognaam = '" . $_POST["inlognaam"] ."'
        AND wachtwoord = '$wachtwoord'";
//voer de query uit, en zet het resultaat in een variabele
    $result = mysql_query($query) or die("FOUT : " . mysql_error());

//als de username en wachtwoord bestaan gaat hij verder
    if(mysql_num_rows($result) > 0){
        include("geheimwoord.inc");
        $hash = md5($inlognaam.$secretword);

//zet gebrukersnaam in een sessie
        session_register("inlognaam");

//zet de md5 hash waarde in een cookie
        Setcookie("userhash", $hash, Time()+3600);

//stuur hem door naar de beveilifde pagina
        header("Location: index2.php");

//als de login gegevens niet kloppen geef je een fout
    }else{
        exit("Foute login gegevens");
    }

//als de pagina niet vanuit de index pagina is aangeroepen ga je erheen
}else{
    header("Location: index.php");
}
?>

inc_connect.php=

<?php
$username = "mailm2";
$password = "(maak ik maar niet openbaar)";
$host = "localhost";
$dbnaam = "mailm2";
$db = mysql_connect($host, $username, $password) or die (mysql_error());
mysql_select_db($dbnaam, $db) or die (mysql_error());
?>
oke... now try removing the error_reporting(E_ALL); from login.php
and change         session_register("inlognaam");

into

$_SESSION['inlognaam'] = $inlognaam;

under:

//voer de query uit, en zet het resultaat in een variabele

add:

$inlognaam = $_POST['inlognaam'];
Avatar of cleany

ASKER

the first message is gone....the other ones are still there...strange...
change

//als de login gegevens niet kloppen geef je een fout
    }else{
        exit("Foute login gegevens");
    }

in:

//als de login gegevens niet kloppen geef je een fout
    }else{
echo $wachtwoord;
echo $loginnaam;
        exit("Foute login gegevens");
    }

and manually compare them with the database values
Avatar of cleany

ASKER

nothing happens...
Avatar of cleany

ASKER

even het aantal punten verhogen...
try to make the password field in the database bigger (max chars to 100)
Avatar of cleany

ASKER

I get the same errormessage...

isn`t it something with the "exit" or something ?
Avatar of cleany

ASKER

it`s something with the "header". It`s called  2 or 3 times or something...
yeah... beacause data is send before the header.. the header can't be executed

show me de complete code of the login.php that you have now
Avatar of cleany

ASKER

<?php
//start de sessie
session_start();

//als de pagina goed is aangeroepen gaan we verder
if(!empty($_POST)){

//include de databse connectie
    include("inc_connect.php");

//encrypt het net verstuurde wachtwoord met md5
    $wachtwoord = md5($_POST["wachtwoord"]);

//zet de query in een variabele
    $query = "SELECT * FROM beheer
        WHERE inlognaam = '" . $_POST["inlognaam"] ."'
        AND wachtwoord = '$wachtwoord'";
//voer de query uit, en zet het resultaat in een variabele
    $result = mysql_query($query) or die("FOUT : " . mysql_error());

            $inlognaam = $_POST['inlognaam'];
//als de username en wachtwoord bestaan gaat hij verder
    if(mysql_num_rows($result) > 0){
        include("geheimwoord.inc");
        $hash = md5($inlognaam.$secretword);

//zet gebrukersnaam in een sessie
$_SESSION['inlognaam'] = $inlognaam;

//zet de md5 hash waarde in een cookie
        Setcookie("userhash", $hash, Time()+3600);

//stuur hem door naar de beveilifde pagina
        header("Location: index2.php");

//als de login gegevens niet kloppen geef je een fout
    }else{
            echo $wachtwoord;
            echo $loginnaam;
        exit("Foute login gegevens");
    }
//als de pagina niet vanuit de index pagina is aangeroepen ga je erheen
}else{
    header("Location: index.php");
}
?>
hij zou nu goed moeten werken... maak je username en wachtwoord nog een keer aan... (oude verwijderen, nieuwe toeveogen)
Avatar of cleany

ASKER

heb ik gedaan....

maar toch is die fout er nog :S
dan snap ik er niets meer van...

je kan dit proberen:

maak ene nieuw php bestand (test.php)

<?
echo md5('jewachtwoord');
?>

vergelijk deze md5 hash met de hash in je database. als die niet gelijk is weet je dat het niet aan je login.php ligt
Avatar of cleany

ASKER

test.php
a9a3b41f618372309ac46ddb71ca067a

en

database:
098f6bcd4621d373cade4e832627b4f6

dit komt in weze overeen, maar is niet het zelfde
Avatar of cleany

ASKER

if(mysql_num_rows($result) > 0)

dit moet zijn

if(mysql_num_rows($result) > 1)
Avatar of cleany

ASKER

hij zegt alleen nog...

Foute login gegevens
Avatar of cleany

ASKER

even 50 erbij
Avatar of cleany

ASKER

oke...ik ben er uit...

het had iets met witregels te maken in de inc_connect.php

ik wist niet dat dat als HTML output gezien wordt....

de 500 points are yours !
thnx mate :) hoop dat je er veel aan hebt... en als er nog eens iets is laat het maar weten
Avatar of cleany

ASKER

had je ook een pagina waar je het wachtwoord kan aanpassen of niet ?
aanpassen niet, wel toevoegen.

Maar als je wil kanik die funtie wel schrijven
Avatar of cleany

ASKER

is dat veel werk ? want je moet wel rekening met de hashing houden he...