Forums

PHP

This topic is locked

Authentication Code Help

Posted 31 May 2006 18:20:31
1
has voted
31 May 2006 18:20:31 Tyger Burch posted:
Im currently working on a authorization script for my site and I found this one in a book I have been studying form. For some reason when you hit submit I get this error:

Fatal error: Cannot instantiate non-existent class: mysql in /usr/home/clients/public_html/backend/authmain.php on line 10

And I can't figure out what I should do. Here's the script, so if anyone can help I would greatly appreciate it.

<?php
session_start();

if (isset($_POST['userid']) && isset($_POST['password']))
{
// if the user has just tried to log in
$userid = $_POST['userid'];
$password = $_POST['password'];

$db_conn = new mysqli('localhost', 'admin', 'ty7admin', 'auth');

if (mysqli_connect_errno()) {
echo 'Connection to database failed:'.mysqli_connect_error();
exit();
}

$query = 'select * from authorised_users '
."where name='$userid' "
." and password=sha1('$password')";

$result = $db_conn->query($query);
if ($result->num_rows >0 )
{
// if they are in the database register the user id
$_SESSION['valid_user'] = $userid;
}
$db_conn->close();
}
?>
<html>
<body>
<h1>Home page</h1>
<?
if (isset($_SESSION['valid_user']))
{
echo 'You are logged in as: '.$_SESSION['valid_user'].' <br />';
echo '<a href="logout.php">Log out</a><br />';
}
else
{
if (isset($userid))
{
// if they've tried and failed to log in
echo 'Could not log you in.<br />';
}
else
{
// they have not tried to log in yet or have logged out
echo 'You are not logged in.<br />';
}

// provide form to log in
echo '<form method="post" action="authmain.php">';
echo '<table>';
echo '<tr><td>Userid:</td>';
echo '<td><input type="text" name="userid"></td></tr>';
echo '<tr><td>Password:</td>';
echo '<td><input type="password" name="password"></td></tr>';
echo '<tr><td colspan="2" align="center">';
echo '<input type="submit" value="Log in"></td></tr>';
echo '</table></form>';
}
?>
<br />
<a href="members_only.php">Members section</a>
</body>
</html>

Replies

Replied 31 May 2006 18:37:45
31 May 2006 18:37:45 Roddy Dairion replied:
Try this out.
<pre id=code><font face=courier size=2 id=code>
&lt;?php
session_start();

if (isset($_POST['userid']) && isset($_POST['password']))
{
// if the user has just tried to log in
$userid = $_POST['userid'];
$password = $_POST['password'];

$db_conn = mysql_connect('localhost', 'admin', 'ty7admin');

if (mysqli_connect_errno()) {
echo 'Connection to database failed:'.mysqli_connect_error();
exit();
}

$query = "select * from authorised_users where name='$userid' and password='$password'";

$result = $db_conn-&gt;query($query);
if ($result-&gt;num_rows &gt;0 )
{
// if they are in the database register the user id
$_SESSION['valid_user'] = $userid;
}
$db_conn-&gt;close();
}
?&gt;
</font id=code></pre id=code>

Edited by - roders22 on 31 May 2006 18:38:09
Replied 02 Jun 2006 05:22:02
02 Jun 2006 05:22:02 Tyger Burch replied:
I get this error now with the script you provided:

Fatal error: Call to undefined function: mysqli_connect_errno() in /usr/home/clients/public_html/backend/authmain.php on line 12

Thanks for your help so tho.
Replied 02 Jun 2006 11:30:23
02 Jun 2006 11:30:23 Roddy Dairion replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
I get this error now with the script you provided:

Fatal error: Call to undefined function: mysqli_connect_errno() in /usr/home/clients/public_html/backend/authmain.php on line 12

Thanks for your help so tho.

<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
<pre id=code><font face=courier size=2 id=code>
&lt;?php
session_start();

if (isset($_POST['userid']) && isset($_POST['password']))
{
// if the user has just tried to log in
$userid = $_POST['userid'];
$password = $_POST['password'];

$db = mysql_connect('localhost', 'admin', 'ty7admin') or die ("Cannot engage connection";
$db_conn = mysql_select_db("auth" or die("Cannot connect to database";

$query = "select * from authorised_users where name='$userid' and password='$password'";

$result = $db_conn-&gt;query($query);
if ($result-&gt;num_rows &gt;0 )
{
// if they are in the database register the user id
$_SESSION['valid_user'] = $userid;
}
$db_conn-&gt;close();
}
?&gt;
</font id=code></pre id=code>

Reply to this topic