Jump to content

[SOLVED] Login form problems


VHPirie

Recommended Posts

Just found a tutorial on how to make a VERY simple forum. Got the forum working, now I want to start adding features. This is just a project to help me learn php.

 

I made the entire login side of things from scratch, and it uses cookies. For now I'm just putting the 'stored' login details in with the file, but when I get the hole login thing sorted I'll save them in the database using md5 encryption.

 

My problem.....I've set it up so it checks the username to see if it's 'on the list'. If it is well everything works ok, but when the username isn't on my list the $password2 variable is undefined which results in an error. I want to make it so once it's checked through the list of usernames, if it didn't come across the username then $password2 is set to nothing...so then it will still be set and won't give me an error.

 

Here's a small bit of code which I tried, but didn't work.

switch ($username) {
     case "bob":
     $password2 = 'qwerty';
     break;
} else {
$password2 = '';
}

 

 

Below is the entire page.

 

<?php
$username = $_POST['username'];
$password = $_POST['password'];

switch ($username) {
     case "bob":
     $password2 = 'qwerty';
     break;
}
if($password == $password2){

setcookie("user", "$username", time()+3600);
echo "<center>Thank you for logging in.<br>Logged in as $username.</center>";
echo "<META HTTP-EQUIV=REFRESH CONTENT=3;URL=index.php>";

}else{
echo "Username or password incorrect.";
}


     
?>

Link to comment
Share on other sites

<?php
$username = $_POST['username'];
$password = $_POST['password'];

$password2 = null; // assign here

switch ($username) {
     case "bob":
     $password2 = 'qwerty';
     break;
   
    default:
         $password2 = null; //or here.
     break; 
}

 

use the default for the case or assign it before you enter the case.

Link to comment
Share on other sites

thanks heaps mate! That worked great...I dunno why I didn't think of that haha....but I guess that's wat learning is all about.

 

I did come up with another way which sort of worked...it would work but wouldn't display the error msg. I added this just b4 it called the $password2 variable:

 

if(isset($password2)){

 

...then closed it off just before the error msg.

 

But no need for that now coz ur idea worked!

 

thanks again buddy! ;D

Link to comment
Share on other sites

thanks heaps mate! That worked great...I dunno why I didn't think of that haha....but I guess that's wat learning is all about.

 

I did come up with another way which sort of worked...it would work but wouldn't display the error msg. I added this just b4 it called the $password2 variable:

 

f(isset($password2)){

 

...then closed it off just before the error msg.

 

But no need for that now coz ur idea worked!

 

thanks again buddy! ;D

 

To enlighten you more, you received a notice, which is simply that a notice. In the newer versions of PHP notices is sent out for variables that are not defined. Generally a good check (and what you should probably do) is use the isset on POST data. The isset is nice, but in the above problem it was not necessary as you could define the variable knowing that the initial value should be blank.

 

For post data I would highly recommend using this to avoid those notices later on.

 

<?php

$username = isset($_POST['username'])?$_POST['username']:'';
$password = isset($_POST['password'])?$_POST['password']:'';

 

Incase you do not know the ? and : are the ternary operator which is setup just like an if statement, just encapsulated.

 

Anyhow good luck.

Link to comment
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.