Sinisterly
PHP Login - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: PHP (https://sinister.ly/Forum-PHP)
+--- Thread: PHP Login (/Thread-PHP-Login)

Pages: 1 2 3 4 5


RE: PHP Login - ObscureCoder - 12-27-2013

I read the other replies and it seems none were very informative, so I'll give it a go.

Your code:
PHP Code:
<?php echo 'Account ID:'; echo '<input type="text" name="user">'; echo 'Password:'; echo '<input type="text" name="pass">'; if(user == 'username' && pass == "password") { echo 'You have successfully logged in' } else { echo 'Incorrect sign in info' } ?>

Your code doesn't actually submit anything from the form as it's not checking whether a variable such as a submit button (HTML) has been set. Also, I realize you are new to PHP, when you send variables via HTML forms you MUST access them using the headers you specified in the form. These headers are known as 'GET' & 'POST'.

GET - This sends variables in the URL, an example of this is URLs like: http://www.somesite.com/index.php?id=23 <- GET (get requests have a limit of how much you can send because URL sizes in browsers are limited)
POST - this is information that is stored server side as variables, not in the URL. Examples of this are when you login into websites, your info such as username and passwords are not shown in the URL of the page. It's hidden thus you can send much more data; more securely.

You define what type of variables you would like to use in your HTML form which you shouldn't echo using PHP.
Code:
<form action="" method="POST"> <!-- Form components here. The action is empty because it goes NOWHERE, the form will send data to the same page. Oh yeah, this is a HTML comment. --> </form>

OK, so now we can add form components to our form. We need 3 of all different types - text, password, submit (button).

Here's how do this:

Code:
<form action="" method="POST"> <input type="text" name="username" /><br /> <input type="password" name="password" /><br /> <input type="submit" name="doLogin" value="Login" /> </form>

So we called our text box 'username', pass box 'password', submit button 'doLogin' (camel casing; good coding technique).
Later, in PHP we will access the data in these components in PHP by using these identifiers.

OK, so now got our form down we need to add the backend stuff. I assume you know how to create variables so since this login doesn't utilize a database such as MySQL etc. then we can set these locally in our script (not very secure but a nice starting place for a beginner). The code I will write has NO form validation such as checking whether anything is entered etc.
Let's begin: (I will write the PHP separate although as mentioned earlier the code WILL be on the same page because the form action goes nowhere)
PHP Code:
<?php // Set our login values as variables. $user = "Test"; // String $pass = "password"; // String ?>

This is the values we will test against, now let's create our action (the code executed when the button is pressed - we say when the button is SET). Before we can do this we must understand SUPERGLOBAL variables. These are how we access form data. Since we said our method was POST (described earlier) we can access it using a rather odd variable like this:

Code:
$_POST['nameOfComponent']

GET variables are the same but with 'GET' instead of 'POST'.
So let's begin creating our action.

The action is an if statement checking whether our (POST) button has been SET. We use a simple function called isset() to check this.

Here we go:

PHP Code:
<?php // Set our login values as variables. $user = "Test"; // String $pass = "password"; // String // Creating our action if(isset($_POST['doLogin'])){ // We check authenticity of credentials here. // Remember we called our submit button doLogin hence the above identifier. } ?>

OK, so once the button is clicked.. set what we entered as variables to then check authenticity.

PHP Code:
<?php // Set our login values as variables. $user = "Test"; // String $pass = "password"; // String // Creating our action if(isset($_POST['doLogin'])){ // Set what the user entered as variables - we don't have to do this technically but is cleaner code. $username = $_POST['username']; // Grab data from input text username. $password = $_POST['password']; // Grab data from input password password. } ?>

Now we do a simple if statement to check whether what the user entered matches our local variables called user and pass then echo results.

PHP Code:
<?php // Set our login values as variables. $user = "Test"; // String $pass = "password"; // String // Creating our action if(isset($_POST['doLogin'])){ // Set what the user entered as variables - we don't have to do this technically but is cleaner code. $username = $_POST['username']; // Grab data from input text username. $password = $_POST['password']; // Grab data from input password password. // Do if check if($username == $user && $password == $pass){ // Success echo "You have logged in."; } else { // Failure - credentials are false. echo "Username and/or password are incorrect! Try again!"; } } ?>

This code is untested but should work fine, if there's errors please tell me as although this reply is pretty long I did type it pretty fast so I haven't done a proof read. I hope this helped, if you need more help with anything PHP.. PM me!


RE: PHP Login - shirofuji - 12-30-2013

Why dont u jst use the standard format

Loginpage.html: *note: you can change the filename of this to anything
Code:
<form action='Login.php' method='post' > Username: <input type="text" name="user" /> Password: <input type="password" name="pwd" /> <input type="Submit" value="Login" /> </form>

Login.php: *note: if you change the filename of this change the action attribute of the form in the first file too.
PHP Code:
<? if($_POST['user']!=null || $_POST['pwd']!=null){ if($_POST['user']=="admin" && $_POST['pwd']=="administrator"){ echo "Login successful!"; } }

Hope this gives you a basic idea of how login works on websites.
I'd be happy to teach you php if you wish.


RE: PHP Login - idiot - 01-23-2014

echo 'Incorrect sign in info'; Smile


RE: PHP Login - VipVince - 01-30-2014

Why not use a database?


RE: PHP Login - ConsoleTVs - 03-03-2014

This should work yeah!

K can help you with a database login and register but here you can get a free one:

http://vexthemes.co.nf/VexLogin/free.php