PDA

View Full Version : Any PHP coders around here?


Joe 90
07-02-2009, 16:13
I'm having issues with errors and i can't stand debugging PHP.

i've written a forum, got all the code, forms, scripts etc sorted. but obviously when you put it up it moans.

http://homepages.shu.ac.uk/~mwalker8/bbad/forum/

The registration works fine, login doesnt :( no idea why at the moment.

main problem though i guess;
Fatal error: Call to undefined function mysqli_connect()

so i'm using that standard sqli function like this;
$con = mysqli_connect($host,$user,$passwd) or die(mysql_error());

does anybody know exactly what this function does? its obviously in compatible with the uni server so i guess i need to write it out myself as it were.


also, theres that login that doesn't work...
so my code;
login form
<?php if (isset($login_error)) { ?>
There was an error: <?php echo $login_error; ?>, please try again.
<?php } ?>
<form action="login_submit.php" method="post">

<b>Username:</b> <input type="text" size="20" maxlength="20" name="username"
<?php if (isset($_POST['Username'])) { ?> value="<?php echo $_POST['Username']; ?>" <?php } ?>/><br />

<b>Password:</b> <input type="password" size="20" maxlength="10" name="Password" /><br />

<input type="submit" name="submit" value="Login" />
</form>

login script;

<?php

// Include start file
include 'start.php';

if (!isset($_POST['submit']))
{
// Show the form
include 'login.php';
exit;
}
else
{
// Try and login with the given Username & pass
$result = user_login($_POST['Username'], $_POST['Password']);

if ($result != 'Correct')
{
// Reshow the form with the error
$login_error = $result;
include 'login.php';
}
else
{
echo 'Thank you for logging in, please <a href="login.php">click here</a>.';
}
}
?>

and those login functions;

function user_login($Username, $Password)
{
// Try and get the rand from the database using the Username
$query = "select rand from forum_Users where Username='$Username' limit 1";
$result = mysql_query($query);
$user = mysql_fetch_array($result);

// Using the rand, encrypt the given Password to see if it
// matches the one in the database
$encrypted_pass = md5(md5($Password).$user['rand']);

// Try and get the user using the Username & encrypted pass
$query = "select UserID, Username from forum_Users where Username='$Username' and Password='$encrypted_pass'";
$result = mysql_query($query);
$user = mysql_fetch_array($result);
$numrows = mysql_num_rows($result);

// Now encrypt the data to be stored in the session
$encrypted_id = md5($user['UserID']);
$encrypted_name = md5($user['Username']);

// Store the data in the session
$_SESSION['UserID'] = $UserID;
$_SESSION['Username'] = $Username;
$_SESSION['encrypted_id'] = $encrypted_id;
$_SESSION['encrypted_name'] = $encrypted_name;

if ($numrows == 1)
{
return 'Correct';
}
else
{
return false;
}
}

function user_logout()
{
session_unset ();
session_destroy ();
}

function is_authed()
{
// Check if the encrypted Username is the same
// as the unencrypted one, if it is, it hasn't been changed
if (isset($_SESSION['Username']) && (md5($_SESSION['Username']) == $_SESSION['encrypted_name']))
{
return true;
}
else
{
return false;
}
}


can anyone see an error? i can't figure it out

Mark
07-02-2009, 16:17
http://uk3.php.net/mysqli_connect

At a guess, your host doesn't have the required module built in to php. Putting the following in an empty file on your host:

<?php
phpinfo();
?>
And then opening that page in a web browser will tell you what you have.

Joe 90
07-02-2009, 16:19
yeah i've stuck that in already...
http://homepages.shu.ac.uk/~mwalker8/bbad/

its on a uni server so its probably a very basic PHP install... and therefore no chance of getting additional libraries etc put on!

leowyatt
07-02-2009, 16:23
try mysql_connect instead of mysqli

Joe 90
07-02-2009, 16:25
ta. will give it a bash.

never actually looked through that php info page... i should really pay a bit more attention :/

not used to bein this new to stuff!

leowyatt
07-02-2009, 16:27
from my use of mysqli it's been a dirty quick method of connecting when not using our framework.

Joe 90
07-02-2009, 16:33
right, yeah that worked. but now all the other mysqli useage is causing problems!!

gonna have to lookup the correct syntax for the alternatives.

thanks leo :)

btw, do you know how to get Dreamweaver (CS4) to colour a .inc file as a php file

i edited extentions.txt last night to include on the php link the INC extention, but it didn't work :(
its so hard just looking at plain text.


___
got a bit further...
http://homepages.shu.ac.uk/~mwalker8/bbad/forum/

just gotta figure out how to pass this connection variable in the mysql_error func

leowyatt
07-02-2009, 16:51
We use Zend for all our php coding so afraid I can't help with Dreamweaver 4 :(

Joe 90
07-02-2009, 17:46
np. i was using phpDesinger2008 but noticed that the quotes (double and single) weren't being saved as 'plain text' so things were breaking.

Mark
07-02-2009, 20:12
mysqli is the new MySQL interface, mysql the old. You can't easily mix and match, so I'm afraid you'll have to convert them all (or get your Uni to provide a more complete PHP installation).

kaiowas
08-02-2009, 22:40
I know it's not the issue you've asked about but from quickly looking at your login code I suggest you go and read about SQL injection. Putting user inputted strings straight into a query without sanitizing them first is just asking for trouble.

Mark
08-02-2009, 22:53
Ooh, good point - well spotted. Leaving SQL un-sanitized is asking for your entire database to be downloaded, tampered with, or just deleted, depending on the mood of the hacker at the time.

jmc41
09-02-2009, 00:17
Might want to go down the $db->query route :)

All about OO then you can amend the connection string in a global class. Plus you can set it up to log execution time and stuff. My 2 cents anyway I've got a feeling I'm about to be flamed for some reason.

Joe 90
09-02-2009, 09:49
ta.

I'd of liked to do it OO but i've never written OOPHP so just kept it procedural.
Too late now to change. deadline is today. :/

suarve
09-02-2009, 14:55
ta.

I'd of liked to do it OO but i've never written OOPHP so just kept it procedural.
Too late now to change. deadline is today. :/

You should have made a simple php or include file with your connection string/details in, that you include at the top of every page.

Joe 90
09-02-2009, 16:07
now that is one thing i did have, and always have.

two include files; dbparams and functions_main (connect & db_select)