/    Sign up×
Community /Pin to ProfileBookmark

Help with basic login form

I have created this basic login form and I’m using this login script from [URL=”https://github.com/panique/php-login-minimal/blob/c0a98755afb5759d1ad2822ec0631625cce3184c/classes/Login.php”]https://github.com/panique/php-login-minimal/blob/c0a98755afb5759d1ad2822ec0631625cce3184c/classes/Login.php[/URL] on my localhost

[code=html]<?php
require ‘classLogin.php’;
if (isset($_POST[‘submit’])) {
loginWithPostData($_POST[‘user_name’], $_POST[‘user_password’]);
}
?>
<!DOCTYPE html>
<html lang=”en”>
<head>
<title>Login</title>
</head>

<body>

<div class=”container”>

<form method=”post”>
<h2>Please sign in</h2>
<input type=”text” class=”form-control” name=”user_name” placeholder=”Email address” value=”<?php if(isset($_POST[‘user_name’])) { echo ($_POST[‘user_name’]);} ?>” >
<input type=”password” class=”form-control” name=”user_password” placeholder=”Password” autofocus>
<button name=”submit” type=”submit”>Sign in</button>
</form>

</div>

</body>
</html>
[/code]

I receive this error message upon hitting the submit button “Fatal error: Call to undefined function loginWithPostData() in /form.php on line 4

I am sort of new to this so my question is how do I make calls to the functions so the credentials can be validated?

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMay 08.2014 — The function is a method of a class. You will need to initialize the class in order to use the function (method).

To initialize a class you do so by using the [B]new[/B] operator and calling the class like a function.[code=php]<?php
class Foo
{
function __construct()
{
// the __construct() or constructor is called when initializing a class (if it has one).
}
}
$foo = new Foo(); // initialize Foo
// now we can access any public properties (variables) and/or methods (functions)
?>[/code]
Copy linkTweet thisAlerts:
@dclampMay 08.2014 — Piggybacking off of the previous post... You have the function name wrong as well. And you are not calling the function correctly.

Initialize the class as explained above.Your code should look something like this:

[code=php]

require 'classLogin.php';
$classlogin = New Login;

$classlogin->dologinWithPostData();
[/code]



Also, looking at that function does not require any parameters... Not sure why you are passing the username and password. Perhaps I am looking at the wrong class?
Copy linkTweet thisAlerts:
@ytesfay80authorMay 08.2014 — 
Also, looking at that function does not require any parameters... Not sure why you are passing the username and password. Perhaps I am looking at the wrong class?[/QUOTE]


What do you mean by that?
Copy linkTweet thisAlerts:
@ytesfay80authorMay 08.2014 — Here is the updated code I used but I still receive an error.

[code=html]<?php
require 'classLogin.php';
$classLogin = new Login;


if (isset($_POST['submit'])) {
$classLogin->doLoginWithPostData();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login</title>
</head>

<body>

<div class="container">

<form method="post">
<h2>Please sign in</h2>
<input type="text" class="form-control" name="user_name" placeholder="Email address" value="<?php if(isset($_POST['user_name'])) { echo ($_POST['user_name']);} ?>" >
<input type="password" class="form-control" name="user_password" placeholder="Password" autofocus>
<button name="submit" type="submit">Sign in</button>
</form>

</div>




</body>
</html>
[/code]



This is the error message I get "Fatal error: Call to undefined method Login::doLoginWithPostData() in /form.php on line 7"

I left the __construct() exactly how it was on the github link. Was I suppose to pass some arguments between the ( )?
Copy linkTweet thisAlerts:
@ytesfay80authorMay 08.2014 — The reason I was getting an undefined method was because the the method was loginWithPostData() instead of doLoginWithPostData().

I corrected it but when I hit the submit button without filling out any of the fields I get the same screen appearing and the errors do not show up. I also try entering a correct user and password but the same screen reappears. Can someone take a look at this code and let me know where I might have messed up at.
[CODE] public function loginWithPostData()
{
// if POST data (from login form) contains non-empty user_name and non-empty user_password
if (!empty($_POST['user_name']) && !empty($_POST['user_password'])) {

// create a database connection, using the constants from config/db.php (which we loaded in index.php)
$this->db_connection = new mysqli('localhost', 'root', '1', 'test');

// change character set to utf8 and check it
if (!$this->db_connection->set_charset("utf8")) {
printf("Error loading character set utf8: %sn", $this->db_connection->error);
}

// if no connection errors (= working database connection)
if (!$this->db_connection->connect_errno) {

// escape the POST stuff
$this->user_name = $this->db_connection->real_escape_string($_POST['user_name']);

// database query, getting all the info of the selected user
$sql = "SELECT user_name, user_password
FROM users
WHERE user_name = '" . $this->user_name . "';";
$checklogin = $this->db_connection->query($sql);

// if this user exists
if ($checklogin->num_rows == 1) {

// get result row (as an object)
$result_row = $checklogin->fetch_object();

// using PHP 5.5's password_verify() function to check if the provided passwords fits to the hash of that user's password
if ($_POST['user_password'] === $result_row->user_password) {

// write user data into PHP SESSION [a file on your server]
$_SESSION['user_name'] = $result_row->user_name;
$_SESSION['user_email'] = $result_row->user_email;
$_SESSION['user_logged_in'] = 1;

// set the login status to true
$this->user_is_logged_in = true;

} else {
$this->errors[] = "Wrong password. Try again.";
}
} else {
$this->errors[] = "This user does not exist.";
}
} else {
$this->errors[] = "Database connection problem.";
}
} elseif (empty($_POST['user_name'])) {
$this->errors[] = "Username field was empty.";
} elseif (empty($_POST['user_password'])) {
$this->errors[] = "Password field was empty.";
}
echo $this->errors;
}
[/CODE]


It is almost the same as the original code except that I added the last line "echo $this->errors;" I also removed the passwordverify() function from the code.
Copy linkTweet thisAlerts:
@ShrineDesignsMay 08.2014 — I looked at the github files more closely. Did you go thru the process of installing it (e.g. setting up the config file and installing the MySQL tables)?

In my honest opinion, the script itself is garbage. It uses the built-in functions for session managements (in conjunction with a database). But uses PHP version checking (which should not be there, due to low-level functionality of the script). unnecessary version enforcing + low-level functionality = bad portability = bad programming.
Copy linkTweet thisAlerts:
@ytesfay80authorMay 09.2014 — I looked at the github files more closely. Did you go thru the process of installing it (e.g. setting up the config file and installing the MySQL tables)?

In my honest opinion, the script itself is garbage. It uses the built-in functions for session managements (in conjunction with a database). But uses PHP version checking (which should not be there, due to low-level functionality of the script). unnecessary version enforcing + low-level functionality = bad portability = bad programming.[/QUOTE]


I didnt set up any config files. I just copied the login script from Github then connected to a database table I created through phpmyadmin.

Do you have any recommendations for a different script or would it be best if I just created through scratch?
Copy linkTweet thisAlerts:
@dclampMay 09.2014 — I never use any 3rd party scripts (besides well known classes such as FPDF and the like).

I prefer to write all my code myself because I know exactly how it works and how to trouble shoot it. I know what every line means and the purpose behind it. Using someone elses code you cant always ensure they are programming with your best interest in mind. Security is the most important thing. If you use someone elses script, you cannot confirm that it is secure and is handling data correctly.

I suggest you come up with your own login script.
Copy linkTweet thisAlerts:
@ytesfay80authorMay 09.2014 — I never use any 3rd party scripts (besides well known classes such as FPDF and the like).

I prefer to write all my code myself because I know exactly how it works and how to trouble shoot it. I know what every line means and the purpose behind it. Using someone elses code you cant always ensure they are programming with your best interest in mind. Security is the most important thing. If you use someone elses script, you cannot confirm that it is secure and is handling data correctly.

I suggest you come up with your own login script.[/QUOTE]


Okay thanks, I am new to oop in php so could you recommend a website that can teach me about writing scripts and best practices? I just need something to get me started so I can have an idea on where to begin.
Copy linkTweet thisAlerts:
@dclampMay 09.2014 — Google found this: http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762

That tutorial looks like a good place to start.
Copy linkTweet thisAlerts:
@Strider64May 09.2014 — You don't have to write a login script in OOP, you still can do it the procedural way. I wrote a basic login/registration tutorial and even have a demo page: http://www.pepster.com/PD0_demo/register.php. I feel it's pretty secure and I'm pretty sure you could modify it to fit your needs, if you choose to do so.
×

Success!

Help @ytesfay80 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...