Jump to content

Login Script Error


Nprew

Recommended Posts

Well I'm completely new to the whole php and mysql aspect of web development and this is my first project. I've got a script from a tutorial that I'm using as a shell for a login system I plan on expounding on after I can get this to work. Currently I've got 3 files:

main_login.php

checklogin.php

login_success.php

 

I set up a table called "members" which stores the ID#, Username, and Password and then set up the files on the site. The main_login.php presents a form to enter the username and password in. Then redirects to checklogin.php to verify the information with the table in the database and is supposed to jump to login_success.php if it checks out or return "invalid username or password" if the info isn't in the table. I put one value (username = john, password = 1234) into the table and when I use it to log in it takes me to login_success.php as if it's cleared but then gives me the error:

 

Notice: Use of undefined constant myusername - assumed 'myusername' in /s/bach/k/under/prew/public_html/login_success.php on line 2

Login Successful

 

I'm not exactly sure why it's displaying this. I went back to checklogin.php and it's showing the same error for both myusername and mypassword. Anyone know what might be causing this? Is it a php problem or something wrong with my table?

 

Thanks,

Nate

 

To see if it will help at all, here's the code as it stands on the site.

 

main_login.php

<form name="form1" method="post" action="checklogin.php">
Username: <input style="margin-right: 5px;" type="text" size="10" maxlength="40" name="myusername" id="myusername" />
Password: <input style="margin-right: 5px;" type="password" size="10" maxlength="10" name="mypassword" id="mypassword" />
<input style="background-color:#222222; border: 1px solid #000000; color: #EFECCA; outline: none;" type="submit" name="Submit" value="Login" />
</form>

 

checklogin.php

<?php
ob_start();
$host="localhost"; // Host name
$username="prew"; // Mysql username
$password="xxxxxx"; // Mysql password
$db_name="prew"; // 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");

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];


$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
if($result){
echo "go";
}

//mysql_num_row is counting table row
$count=mysql_num_rows($result);
//if result match  $myusername  and mypassword  table row must be 1 row

if($count==1){
session_register("myusername");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>

 

login_success.php

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

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

Link to comment
https://forums.phpfreaks.com/topic/130643-login-script-error/
Share on other sites

On checklogin, you will want to put session_start() at the top and change your session_register to $_SESSION

 

I made the changes, or what I believe you were saying needed to be changed but now I've got a fatal error at the first $_SESSION. Sorry if I'm coming across as being completely dense, I usually try not to ask to have my hand held through this stuff but after several hours of pounding my head against a seemingly unmovable wall, I figured it's time to seek outside assistance.

 

This is what I did to the code:

 

<?php
session_start();
ob_start();
$host="localhost"; // Host name
$username="prew"; // Mysql username
$password="xxxxxx"; // Mysql password
$db_name="prew"; // 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");

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];


$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
if($result){
echo "go";
}

//mysql_num_row is counting table row
$count=mysql_num_rows($result);
//if result match  $myusername  and mypassword  table row must be 1 row

if($count==1){
$_SESSION("myusername");
$_SESSION("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>

Link to comment
https://forums.phpfreaks.com/topic/130643-login-script-error/#findComment-677880
Share on other sites

$_SESSION["myusername"] = $myusername;

$_SESSION["mypassword"] = $mypassword;

 

 

If you remove your echo "go", you can remove your ob's.

 

hehe back to the same error I had at the beginning:

 

Notice: Use of undefined constant myusername - assumed 'myusername' in /s/bach/k/under/prewitt/public_html/login_success.php on line 2

Link to comment
https://forums.phpfreaks.com/topic/130643-login-script-error/#findComment-677891
Share on other sites

You can try a different approach:

 

I use a login script much like the one on the about page, with some modifications:

http://php.about.com/od/finishedphp1/ss/php_login_code.htm

 

Others who I have recommended it to have found it very easy to understand and manipulate.

 

Try it out!

Link to comment
https://forums.phpfreaks.com/topic/130643-login-script-error/#findComment-677918
Share on other sites

You can try a different approach:

 

I use a login script much like the one on the about page, with some modifications:

http://php.about.com/od/finishedphp1/ss/php_login_code.htm

 

Others who I have recommended it to have found it very easy to understand and manipulate.

 

Try it out!

 

That was so much simpler, I got it up and running. Thanks for all of the help :D and also just out of curiosity if someone knows what the problem with the previous set up was I'd love to know.

 

Link to comment
https://forums.phpfreaks.com/topic/130643-login-script-error/#findComment-677946
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.