Forum Moderators: coopster

Message Too Old, No Replies

Login Script

Not loging in

         

togethercomms

9:46 am on Jun 26, 2009 (gmt 0)

10+ Year Member



I have a script that i done but the problem is that the script always brings up the error message and i know its the right username and password.
It won't log in to the member page.

Any help woould be great.
script below;

<?php
//start the session
session_start();

// db properties
$dbhost = '#*$!#*$!#*$!x';
$dbuser = '#*$!#*$!#*$!xx';
$dbpass = '#*$!#*$!#*$!#*$!';
$dbname = '#*$!#*$!#*$!#*$!';

// make a connection to mysql here
$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db ($dbname) or die ("There is an error with '$dbname' this has been noted, please check back later");

//log user in ---------------------------------------------------
function login($user, $pass){

// escape all data in variables to prevent mysql injection
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);

$pass = md5($pass);

// check if the user id and password combination exist in database
$sql = "SELECT username FROM members WHERE username = '$user' AND password = '$pass'";
$result = mysql_query($sql) or die('<font color="red">You are not authorised to access the database.</font> ' . mysql_error());

if (mysql_num_rows($result) == 1) {
// the username and password match,
// set the session

//get the memberID from database
$getid = mysql_query("SELECT * FROM members WHERE username = '$user'");
while($row = mysql_fetch_object($getid)){

//assign memberID to a variable
$memberID = $row->memberID;

//set the session
$_SESSION['isloggedin'] = $memberID;
}

// reload the page
header("Location: http://www.example.co.uk/test/member-area.php");
exit;
} else {
//make error message avalible outside of function
global $errorMessage;

// define an error message
$errorMessage = '<p><font color="red">The password OR username provided was not recognised</font></p>';
}
}
?>

<?php
if (!isset($_SESSION['isloggedin'])){
?>

<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<p>Login: Username
<input name="user" type="text" size="10" />&nbsp; &nbsp; &nbsp;
Password
<input type="password" name="pass" size="10" /><br><br>
<input type="submit" name="slogin" value="Login" />
</p>
</form><br><br>
<?php
}
if (isset($_POST['slogin'])){
login($user, $pass);
}

//if login failed
if (isset($errorMessage)) {
echo "<p><span class=\"warning\">$errorMessage</span></p>\n";
}
?>

[edited by: coopster at 1:15 pm (utc) on June 26, 2009]
[edit reason] please use example.com, thanks! [/edit]

jatar_k

1:35 pm on Jun 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



which error does it always bring up?

have you looked through each test to isolate which specific part of your script is failing?

togethercomms

2:06 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



i managed to sort it out thanks, it was a faulty script anyhow, don't always trust a script from the internet lol thanks tho

togethercomms

2:08 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



actually, i know this is the wrong subject but it seems a wasted thread if i open a new one,

I was wondering how i could add 2 buttons to this script. 1 that would delete the record and the other that would amend the record

<?php
$mysqli = mysqli_connect("#*$!#*$!#*$!#*$!#*$!#*$!");

if (mysqli_connect_errno()) {
printf("Connect Failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "SELECT * FROM job_board";
$res = mysqli_query($mysqli, $sql);

if($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['id'];
$title = $newArray['title'];
$short = $newArray['short'];
$salary = $newArray['salary'];
$end_date = $newArray['end_date'];
echo "<div id='border'>";
echo "<div class='bg'><div class='subject'>".$title. " &pound;" .$salary."</div></div>";
echo "<div class='body'>".$short."<div class='morelink'><a href=\"template/job-info.php?id=".$id."\">I'm Interested</a></div></div>";
echo "<div class='bg'><div class='footer'><a href='mailto:info@example.co.uk'>Click to respond</a></div> </div>";
echo "</div><br>";
}
} else {
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
mysqli_free_result($res);
mysqli_close($mysqli);
}
?>

[edited by: jatar_k at 2:15 pm (utc) on June 26, 2009]

jatar_k

2:17 pm on Jun 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



mainly you just add them, often each in it's own form if you really want to use a button or just append the id or something if it is just a link.

each form would post to a script that would perform the function you want. Or to a single form that has some kind of switch to do the different functions.

togethercomms

2:19 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



well i have the insert form and script and i just want a button that will amend and another for deleting, but for some reason i can't figure it out... gone blank

jatar_k

2:30 pm on Jun 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



for edit you use your insert form but load the data for the specific row or record you want to edit. So you could do an if for testing if the form is fed an id. If the id is there load data

for delete you really just run a one liner with error checking to make sure someone doesn't use it to delete more than you intended

togethercomms

3:33 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



how would i load the data back into the text boxes for edit?
and for the delete would this work;

DELETE * FROM job_board WHERE id = $id;

Many Thanks

jatar_k

3:44 pm on Jun 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you don't need the *
DELETE FROM job_board WHERE id = $id;

you would select the data from you table

select * from job_board where id = $id;

then just load your vars from the row and echo them into the form elements in the value="<?php echo $myrowvalue; ?>" or something like that