Jump to content

[SOLVED] SIMPLE LOGIN PHP SCRIPT - MYSQL ERROR.


cmbcorp

Recommended Posts

Hi,

 

I have been playing around with a simple php login script and im getting an error message when i attempt to log in with the username and password i set in the sql table

 

Below is the code im using:

 

 

Code: ( php )

<?php

$host="localhost"; // Host name

$username="greenpos_admin1"; // Mysql username

$password="carlo"; // Mysql password

$db_name="greenpos_test"; // Database name

$tbl_name="members"; // Table name

 

// Connect to server and select databse.

// Connect to server and select databse.

$link=mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db($db_name, $link)or die("cannot select DB");

 

 

// username and password sent from signup form

 

$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_start();

$_SESSION['myusername']=$myusername;

$_SESSION['mypassword']=$mypassword;

header("location:login_success.php");

exit;

}

else {

echo "Wrong Username or Password";

exit;

}

?>

 

 

This is what i did to create the table in mysql:

 

 

Code: ( php )

CREATE TABLE `members` (

`id` int(4) NOT NULL auto_increment,

`username` varchar(65) NOT NULL default '',

`password` varchar(65) NOT NULL default '',

PRIMARY KEY (`id`)

) TYPE=MyISAM AUTO_INCREMENT=2 ;

 

 

I then:

Dumped the data for table `members`

 

 

Code: ( php )

INSERT INTO `members` VALUES (1, 'jason', 'jason'');

 

 

Now when i use my login screen and type in my username and password

 

i get this error message:

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 37

 

Warning: Cannot modify header information - headers already sent by (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 40

 

line 37:

session_start();

 

Line 40:

header("location:login_success.php");

 

Im not sure if im diong everything right...

 

The code for my main_login.php:

 

 

Code: ( php )

<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> </td>

<td> </td>

<td><input type="submit" name="Submit" value="Login"></td>

</tr>

</table>

</td>

</form>

</tr>

</table>

 

 

All i can think of is that when i added the table members to my database, i added username and password as the fields and not myusername and mypassword?

 

Please i would really appreciate your help.

 

Thanks.

Jason.

Link to comment
Share on other sites

Right at the top of the page before all code add:

<?

ob_start();

?>

 

and at the end of the page after all code add:

 

<?

ob_end_flush();

?>

 

Hi,

 

Thanks soooooooo much for your reply.

 

I had added that to the start of the code and the other to the end.

 

I think it may have worked.. It does display Login Successfull. But i still get a error message (which is new mind you).

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/greenpos/public_html/test4/login_success.php:2) in /home/greenpos/public_html/test4/login_success.php on line 3

Login Successful

 

Please advise your thoughts.

 

Cheers,

Jason.

 

 

Link to comment
Share on other sites

Can you post the code for login_success.php?

 

Thanks once again.

 

Code for login_success.php is:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?

session_start();

if(!session_is_registered(myusername)){

header("location:main_login.php");

}

?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

Login Successful

</body>

</html>

Link to comment
Share on other sites

try

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
exit();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
Login Successful
</body>
</html>

Link to comment
Share on other sites

try

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
exit();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
Login Successful
</body>
</html>

 

HI thanks once again for your help! you guys are great!.

 

unfortunatly i replaced that code and the error message appears:

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/greenpos/public_html/test4/login_success.php:2) in /home/greenpos/public_html/test4/login_success.php on line 3

Login Successful

 

But im gathering it is working so far apart from the error.

 

Cheers,

Jason.

Link to comment
Share on other sites

try

<?

session_start();

if(!isset($_SESSION['myusername']){

header("location:main_login.php");

exit();

}

?>

 

note you have to put that at the very top of the page without any white space

i dont see any error with that so try to check if the white space is in the top

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.