Jump to content

Recommended Posts

hi, this is my login function, when i try to login with my login details it says that they are invalid.

 

the code isnt communicating with my database, i looked at the query and it looks ok to me.

 

can someone help me please?

 

function logging_in()
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));

if (empty($username) || empty($password))
{
echo "You havent filled all the required fields<br/><br/><a href='javascript:history.go(-1)'>Go Back</a>";
}
else
{
mysql_connect("localhost", "***", "***")or die("cannot connect");
mysql_select_db("***")or die("cannot select DB");

$query = "SELECT * FROM forumusers WHERE username='$username' AND password='$password'";
$result = mysql_query($query) OR DIE("error: ".mysql_error());
mysql_close();
if (mysql_num_rows($result) > 0) {
$r = mysql_fetch_assoc($result);

$user = $r["username"];
$pass = $r["password"];
if ($username == $user && $password == $pass) {
$loggedin = TRUE;
$_SESSION["username"] = $username;
$_SESSION["access_level"] = $r["access_level"];
$_SESSION["logged_in"] = 1;
echo "Sucessfully Logged In<br/><br/><a href='index.php'>Home</a>";
}
} else {
$loggedin = FALSE;
echo "Invalid Username And Password<br/><br/><a href='javascript:history.go(-1)'>Go Back</a>";
}
}
}

Link to comment
https://forums.phpfreaks.com/topic/94530-login-problem/
Share on other sites

change

<?php
$result = mysql_query($query) OR DIE("error: ".mysql_error());
?>

to

<?php
$result = mysql_query($query) or die (mysql_error()."<br /><br />".$query);
?>

 

Then go into phpmyadmin (or your sql manager) and run that sql query outputted (if it errors) in there to verify that your data is correct.

 

You don't need the

<?php
$user = $r['username'];
$pass = $r['password'];
?>

because you already verified it by saying

<?php
if(mysql_num_rows($result)>0){
?>

the $loggededin = TRUE is pointless also

 

also don't see a session_start(); on the page to initialize sessions

 

just a few things I saw

Link to comment
https://forums.phpfreaks.com/topic/94530-login-problem/#findComment-484030
Share on other sites

you can not use the $_POST within the function, you need to pass the values to the function when you call it

 

start your function as

function logging_in($username,$password){

 

delete the next  lines

$username = mysql_real_escape_string($_POST['username']);

 

EDIT: you need to convert to md5

$password = md5(mysql_real_escape_string($password));

call the function like so

 

<?php

logging_in($_POST['username'],$_POST['password']);
?>

Link to comment
https://forums.phpfreaks.com/topic/94530-login-problem/#findComment-484032
Share on other sites

you can not use the $_POST within the function, you need to pass the values to the function when you call it

 

Super globals are modifiable and accessable inside functions declared globals such as

<?php
$hi = "Hello World.";
function do_text(){
echo $hi;
}
do_text();
?>

That will fail but say $_POST['hi'] = "Hello World.";

<?php
function do_text(){
echo $_POST['hi'];
}
do_text();
?>

That will work because you can get to superglobals of $_REQUEST, $_SESSION, $_POST, $_GET

Link to comment
https://forums.phpfreaks.com/topic/94530-login-problem/#findComment-484034
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.