PHP Login

This is a discussion on "PHP Login" within the PHP Forum section. This forum, and the thread "PHP Login are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 7th, 2007, 13:54
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
PHP Login

Hey all. Following this tutorial:
http://www.phpeasystep.com/workshopview.php?id=6

1. Am I using PHP 4 or PHP 5? How can I find out?
2. Try going here: http://www.jackfranklin.co.uk/PHP%20...main_login.php
and use the username john and the password 1234.

I get lots of errors! The code for the check_login.php is:
PHP: Select all

<?php
$host
="localhost"// Host name 
$username="jackfran_admin"// Mysql username 
$password="<my password>"// Mysql password 
$db_name="jackfran_webdatabase"// Database name 
$tbl_name="members"// Table name 

// Connect to server and select databse.
mysql_connect("$host""$username""$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo 
"Wrong Username or Password";
}
?>
And the page with the form looks like this:
Code: Select all
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
Any help welcome


Thanks

Jack
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 7th, 2007, 15:51
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Login

What are the errors mate? They can help a lot in the debugging process.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 7th, 2007, 16:03
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Login

There are no errors in the PHP script. HTML is also OK. Check the SQL login data....

Otherwise I can't see any problems.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 7th, 2007, 16:26
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Re: PHP Login

The warnings I get are:

Warning: session_register() [function.session-register]: Cannot send session cookie - headers already sent by (output started at /home/jackfran/public_html/PHP Stuff/PHPLogin/checklogin.php:9) in /home/jackfran/public_html/PHP Stuff/PHPLogin/checklogin.php on line 35

Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /home/jackfran/public_html/PHP Stuff/PHPLogin/checklogin.php:9) in /home/jackfran/public_html/PHP Stuff/PHPLogin/checklogin.php on line 35

Warning: Cannot modify header information - headers already sent by (output started at /home/jackfran/public_html/PHP Stuff/PHPLogin/checklogin.php:9) in /home/jackfran/public_html/PHP Stuff/PHPLogin/checklogin.php on line 37

Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 7th, 2007, 16:27
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Login

I'd just like to suggest filtering the input, otherwise this script is vulnerable to SQL Injection i think changing the post part to the following should work:
PHP: Select all

 $myusername=addlashes(htmlentities($_POST['myusername']));  $mypassword=addlashes(htmlentities($_POST['mypassword'])); 

Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 7th, 2007, 18:24
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Login

OK, I got it:

Your problem is here:
PHP: Select all

session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php"); 
You can't output anything before you try to set header information or start a session.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Oct 8th, 2007, 06:33
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: PHP Login

Would it next
PHP: Select all

 session_start() 

At the Top
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Oct 8th, 2007, 16:14
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Re: PHP Login

Thanks for the help guys. What can I do to correct it? simon- I dont quite understand what you are saying?

Cheers
Jack
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Oct 8th, 2007, 16:27
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: PHP Login

Thats how you start a session
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Oct 8th, 2007, 16:34
Marc's Avatar
Staff Manager

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Posts: 1,788
Thanks: 0
Thanked 16 Times in 16 Posts
Re: PHP Login

I think Simon means put this in:

PHP: Select all

session_start();
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php"); 
__________________
Marc
Staff Manager - Webforumz.com


Want to be a moderator? PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Oct 8th, 2007, 17:50
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Login

OK, Let me explain in what I hope is "normal person" english:

The headers of a HTML page get sent before ANY of the content. So, if you want to set any information in the header using PHP (ex: using the header() function or the session_*() functions), you have to do it before you output anything to the page (using echo() or print() or normal HTML output). So, when you try to start a session and change the header, PHP throws an error, because the header has already been sent to the client and the server can't make any more changes.

Hope that makes at least a bit of sense, feel free to throw down some more ?'s....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Oct 8th, 2007, 18:51
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Re: PHP Login

I think I get it. So would I put the code Marc posted at the very very top of my page, before any other code? or would I move all the code before the <html....?

Thanks for the help!

Jack
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Oct 8th, 2007, 19:48
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Login

that's it! you catch on fast...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Oct 8th, 2007, 20:16
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Re: PHP Login

Great Thanks, I'll try it when I get the chance and report back...
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Oct 8th, 2007, 21:18
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Login

Might I just say, it would be a lot easier to put Marc's code in file and "include()" it in every page.
That way, if you need to add or change something, you simply change one file.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Oct 8th, 2007, 21:20
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: PHP Login

All the php on my sites is include
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #17  
Old Oct 9th, 2007, 19:25
Jack Franklin's Avatar
Moderator

SuperMember