Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » PHP form password with MD5
Closed Thread
Old 03-26-2009, 04:52 AM   #11 (permalink)
murdocsvan's Avatar
 
Ultra Techie

Join Date: Jun 2007

Location: Surrey, UK

Posts: 849

murdocsvan is on a distinguished road

Default Re: PHP form password with MD5

Awesome, thanks. Can you see anything i've missed out?
__________________

murdocsvan is offline  
Old 03-26-2009, 07:16 AM   #12 (permalink)
murdocsvan's Avatar
 
Ultra Techie

Join Date: Jun 2007

Location: Surrey, UK

Posts: 849

murdocsvan is on a distinguished road

Default Re: PHP form password with MD5

I was also reading about sessions and i read that the old session has been depracated by $_SESSION. I've tried changing it, but i've found i've got a parse error when i request the session on the other pages. No problem with registering it though.

This is the code that gives me an error:

PHP Code:
<?php

session_start
();

if(!isset(
$_SESSION['logged'))
    {
    
header("location:login/main_login.htm");
    }
    
?>
And this is the code that sets the variable on the check_login page:

PHP Code:
if($count==1)
        {
        
// Register $myusername, $mypassword and redirect to file "login_success.php"
        
$_SESSION['logged'] = "yes";
        
header("location:login_success.php");
        }
    else 
        {
        echo 
"Error: Wrong Username or Password";
        } 
Lol it probably isn't very secure.

Thanks in advanced.
__________________

murdocsvan is offline  
Old 03-26-2009, 03:05 PM   #13 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: PHP form password with MD5

Quote:
Originally Posted by murdocsvan View Post
I was also reading about sessions and i read that the old session has been depracated by $_SESSION. I've tried changing it, but i've found i've got a parse error when i request the session on the other pages. No problem with registering it though.

This is the code that gives me an error:

PHP Code:
<?php

session_start
();

if(!isset(
$_SESSION['logged'))
    {
    
header("location:login/main_login.htm");
    }
    
?>
And this is the code that sets the variable on the check_login page:

PHP Code:
if($count==1)
        {
        
// Register $myusername, $mypassword and redirect to file "login_success.php"
        
$_SESSION['logged'] = "yes";
        
header("location:login_success.php");
        }
    else 
        {
        echo 
"Error: Wrong Username or Password";
        } 
Lol it probably isn't very secure.

Thanks in advanced.
I don't see anything that would give an error. Can you post the error you get?

Also, instead of doing $_SESSION['logged'] = "yes", make it a Boolean. $_SESSION['logged'] = true.

Then, when you check for it, do if ($_SESSION['logged'] == true) { //asdfasdf }
__________________

Need website help? PM me!
CrazeD is offline  
Old 03-26-2009, 03:25 PM   #14 (permalink)
murdocsvan's Avatar
 
Ultra Techie

Join Date: Jun 2007

Location: Surrey, UK

Posts: 849

murdocsvan is on a distinguished road

Default Re: PHP form password with MD5

haha turned out i was just using the wrong type of bracket, if you look at $_SESSON i put ['logged') instead of ['logged']. Most of the time it seems like really simple stuff like this is what catches me out lol.
__________________

murdocsvan is offline  
Old 03-26-2009, 06:12 PM   #15 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: PHP form password with MD5

Oh, I see now. Actually you just forgot the ], the ) should be there.

if(!isset($_SESSION['logged']))

Like that.
__________________

Need website help? PM me!
CrazeD is offline  
Old 03-28-2009, 02:59 PM   #16 (permalink)
murdocsvan's Avatar
 
Ultra Techie

Join Date: Jun 2007

Location: Surrey, UK

Posts: 849

murdocsvan is on a distinguished road

Default Re: PHP form password with MD5

Okay well that's all working dandy now.

A couple of questions. First of all, when i log in, sometimes it says successful, but then takes me back to the login page anyway.

Also, do i have to put this bit code on every page i want protected by the login system:

PHP Code:
//Check user is logged in
session_start();

if(!isset(
$_SESSION['logged']))
    {
    
header("location:login/main_login.htm");
    } 
or is there a simpler way to protect all my pages?
__________________

murdocsvan is offline  
Old 03-28-2009, 03:50 PM   #17 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: PHP form password with MD5

Personally, I like to use flow control for the entire section that I want protected.

For example;

PHP Code:
<?php

session_start
();

if (isset (
$_SESSION['logged'])) {
     
// do logged in stuff
} else {
     
// not logged in
     
header('location: login/main_login.html');
}

?>
Since the header() function just sends headers to the browser, there is probably a way to block or manipulate that data (though, I'm just guessing here) so if that were the case, your script offers no protection. My script displays the logged in stuff ONLY if they are logged in.

Remember that when you are making scripts such as these, always code as if every user is a malicious user and will attempt to use the script in ways you didn't intend.
__________________

Need website help? PM me!
CrazeD is offline  
Old 03-28-2009, 07:13 PM   #18 (permalink)
murdocsvan's Avatar
 
Ultra Techie

Join Date: Jun 2007

Location: Surrey, UK

Posts: 849

murdocsvan is on a distinguished road

Default Re: PHP form password with MD5

Are you sure about that? It just looks like it does the same thing. Also if i used that code on a document with HTML, i'd have to put Echo before every line of HTML code
__________________

murdocsvan is offline  
Old 03-28-2009, 07:17 PM   #19 (permalink)
murdocsvan's Avatar
 
Ultra Techie

Join Date: Jun 2007

Location: Surrey, UK

Posts: 849

murdocsvan is on a distinguished road

Default Re: PHP form password with MD5

And also, how would i protect a file which i can't put PHP in, life a .txt or a .pdf? Isn't there a way to just protect a sub directory?
__________________

murdocsvan is offline  
Old 03-28-2009, 11:08 PM   #20 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: PHP form password with MD5

You don't need to echo every line, you can echo once and then put all of your HTML inside. Or, you can just end the PHP and then continue it later.

PHP Code:
   <?php

session_start
();

if (isset (
$_SESSION['logged'])) {
     echo 
'<b>Multiple</b>

     <i>lines of</i>

     <u>wonderful HTML!</u>'
;
} else {
     
// not logged in
     
header('location: login/main_login.html');
}

?>
PHP Code:
   <?php

session_start
();

if (isset (
$_SESSION['logged'])) {
     
// do logged in stuff

?>

You've ended the PHP tags, so do your junk here.

<?php

// then you can restart here. PHP doesn't care
} else {
     
// not logged in
     
header('location: login/main_login.html');
}

?>
Here's examples of both methods.

To protect an external file, you have two fairly easy methods. One is to do funky things with .htaccess. Another option is to store the files in a folder that's not an obvious name. Then, have long random file names for the files. Store the names in a database and then when the file is requested, just get it from the database. You could even have the name change on each request, or after a certain time period.

It's difficult to truly protect external files, I don't really know of any other way except what I just mentioned.
__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP form murdocsvan Programming Discussions 7 03-11-2009 11:00 PM
DBD::mysql Perl module install CrazeD Linux, BSD, other *nixes & Open Source Software 4 02-28-2009 05:37 PM
PHP - what it does and what it doesn’t Osiris Programming Discussions 1 02-16-2009 04:09 PM
Looking for php form input and display script linux1880 Programming Discussions 3 06-03-2008 10:23 PM
Need PHP and JavaScript Form Validation Scripts aetherh4cker Programming Discussions 2 01-06-2008 04:16 PM