17 replies [Last post]
OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

Quote:

The first thing you will need for this is the MySQL table that will hold the login information. For the scope of this article each record will only hold three pieces of information:

Table: users
Column Name Type Null Primary Key Extra
user_id int(8 ) No PK AUTO
username varchar(11) No
password varchar(32) No

Once we have the table created, now we need to populate it with some user information.

INSERT INTO users (username, password) VALUES (‘someUser’, md5(‘somePass’));

The username and password values can be whatever you want tlhem to be. The md5() function is built into PHP, and will convert your password into a 32 character string. This is one good method for encrypting password information. Whenever you use this, though, you should be careful. The conversion is one-way, and you cannot decrypt your password to read it.

Could somone tell me how to do this?

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

Tags:
Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 4 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

MySQL question

Hi OPTAdmin,
The sql to create the table would becreate table users (user_id int(8) Primary key not null auto increment, username varchar(11) not null, password varchar(32) not null) You need to log onto mysql with your username, password and hostname usually 'localhost' this can be done on the command line or through PHP.

<?php 
/* connect to MySQL */
mysql_connect("mysql_host", "mysql_user", "mysql_password"); 

/* select the DB */
mysql_select_db("my_database"); 

$query="create table users (user_id int(8) Primary key not null auto increment, username varchar(11) not null, password varchar(32) not null)"; 

/* execute the query */
mysql_query($query); 
?>

I always use http://www.appcreator.com/tablemgr.php to create and manipulate MySQL tables it is a simple web interface to MySQL created by a really great and talented guy, Me Wink .

Let me know if you need any more help

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Of course I need help. Laughing out loud

It asks me to create a new database. Based on the information I've given you, does it matter what the name should be? Thanks!

I'm such a noob...

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 4 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

MySQL question

Hi OPTAdmin,
You can choose whatever you like as the database name as long as it doesn't conflict with anything.

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

For some reason, your program wouldn't let me log in.

So, I saw that my server let me us phpMySQL, and I've used that to create the table 'users' with the fields. Now, I'm trying to run the command. The example says:

Quote:

INSERT INTO users (username, password) VALUES (‘someUser’, md5(‘somePass’));

So does that mean I need to change the "someUser" to a user and "somePass" to that user's corresponding password?

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 4 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

MySQL question

Hi OPTAdmin,
Did you set up the connection values correctly?

Yes you change those values to a username and password.

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Okay, I got it all set up, but when I try to log in with the username and password I gave it, I got this error:

Quote:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/admin1/public_html/login.php:7) in /home/admin1/public_html/login.php on line 92

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/admin1/public_html/login.php:7) in /home/admin1/public_html/login.php on line 92
Success!
Warning: Cannot modify header information - headers already sent by (output started at /home/admin1/public_html/login.php:7) in /home/admin1/public_html/login.php on line 99

Here's the code itself, with passwords edited out...

<!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>
<title>Login :: P'burg Express Lines</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="content-style-type" content="text/css" />
<meta name="author" content="OhioPrepTalk.com" />
<meta name="copyright" content="Copyright (c) 2004, OhioPrepTalk.com" />
<link rel="STYLESHEET" href="css/style.css" type="text/css" />
</head>
<body>

<div id="body">

<div id="header">
<h1>P'burg Express Lines</h1>
</div> <!-- header -->

<div id="leftcontent">

<div id="menu">
<ul>
	<li><a href="index.html">company overview</a>
		<ul>
			<li>about our company</li>
		</ul>
	</li>
	<li><a href="login.html">monthly calendar</a>
		<ul>
			<li>calendar of transactions</li>
		</ul>
	</li>
	<li><a href="contact.html">contacting us</a>
		<ul>
			<li>how you can reach us</li>
		</ul>
	</li>
</ul>
</div> <!-- menu -->

<div id="announce">
<h3>announcements</h3>
<p>8/24 - website created</p>
</div> <!-- announce -->

</div> <!-- leftcontent -->

      <div class="contentblock"> 
        <h4>login</h4>
		<?PHP
//check that the user is calling the page from the login form and not accessing it directly
//and redirect back to the login form if necessary
if (!isset($username) || !isset($password)) {
header( "Location: http://www.pburgexpresslines/login.html" );
}
//check that the form fields are not empty, and redirect back to the login page if they are
elseif (empty($username) || empty($password)) {
header( "Location: http://www.pburgexpresslines.com/login.html" );
}
else{

//convert the field values to simple variables

//add slashes to the username and md5() the password
$user = addslashes($_POST['username']);
$pass = md5($_POST['password']);


//set the database connection variables

$dbHost = "localhost";
$dbUser = "admin1_tony";
$dbPass = "******";
$dbDatabase = "admin1_logindb";

//connet to the database

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

$result=mysql_query("select * from users where username='$user' AND password='$pass'", $db);

//check that at least one row was returned

$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
while($row = mysql_fetch_array($result)){

  //start the session and register a variable

  session_start();
  session_register('username');

  //successful login code will go here...
  echo 'Success!';

  //we will redirect the user to another page where we will make sure they're logged in
  header( "Location: checkLogin.php" );

  }

  }
  else {

  //if nothing is returned by the query, unsuccessful login code goes here...

  echo 'Incorrect login name or password. Please try again.';
  }
  }
  ?> 
      </div>
      <!-- contentblock -->
      

  <div id="footer"> 
    <p>Copyright &copy; 2004, <a href="mailto:[email protected]" title="Email Webmaster">A. Fred</a></p>
  </div>
</div> <!-- body -->

</body>
</html>

Any help?

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 4 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

MySQL question

Hi OPTAdmin,
Those errors are for writing data to the screen before starting the session.
You should be able to fix it by starting the session before the doctype.
Try to put all the php session and header redirection stuff at the top before the doctype and any php output, such as echo, in the body where you want it to be displayed.

Is this a tutorial you are going through? it looks a little confusing from my end.
If you get really stuck PM me and I will do what I can.

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Yes, the tutorial can be found here:

http://www.devarticles.com/c/a/PHP/PHP-for-Beginners-by-a-Beginners/1/

I'll work on getting the output and everything working then get back to you.

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Okay, I got it to work the way I wanted it to. But, from there, how would I add a new user? I try using the same line of code...

INSERT INTO users (username, password) VALUES (‘someUser’, md5(‘somePass’));

... but using different values for "someUser" and "somePass," but it doesn't work.

I get this error:

Quote:
#1054 - Unknown column 'someUser' in 'field list'

Any help with adding new members?

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 4 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

MySQL question

Hi OPTAdmin,
If you are doing that in php try:

$pass=md5(‘somePass’);  
$query="INSERT INTO users VALUES ('0', ‘someUser’, '$pass')";  
mysql_query($query);

the '0' is for the userid field and will be incremented to the next available number via auto increment.

Hope that helps

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Okay, I put that into the PHP code on a create user page, and then, in order to access the database, I put (with the correct password, of course)...

<?PHP

$dbHost = "localhost";
$dbUser = "admin1_tony";
$dbPass = "******";
$dbDatabase = "admin1_logindb";

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database."); 

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database."); 

$pass=md5(‘somePass’); 
$query="INSERT INTO users VALUES ('0', ‘someUser’, '$pass')"; 
mysql_query($query);

?>

Or would I need to put more code into it? By the way, thanks so much for your help thus far, I really appreciate it!

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Okay well I did it within mySQL again. I just added a row into the table, and it seems to be working.

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Okay, for some reason there is a parse error with my login page. The error states:

Quote:

Parse error: parse error, unexpected $ in /home/admin1/public_html/login.php on line 129

Here's the code for the page, with the password asterisk'ed out

<?PHP
//check that the user is calling the page from the login form and not accessing it directly
//and redirect back to the login form if necessary
if (!isset($username) || !isset($password)) {
header( "Location: http://www.pburgexpresslines/login.html" );
}
//check that the form fields are not empty, and redirect back to the login page if they are
elseif (empty($username) || empty($password)) {
header( "Location: http://www.pburgexpresslines.com/login.html" );
}
else{

//convert the field values to simple variables

//add slashes to the username and md5() the password
$user = addslashes($_POST['username']);
$pass = md5($_POST['password']);


//set the database connection variables

$dbHost = "localhost";
$dbUser = "admin1_tony";
$dbPass = "*******";
$dbDatabase = "admin1_logindb";

//connet to the database

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

$result=mysql_query("select * from users where username='$user' AND password='$pass'", $db);

//check that at least one row was returned

$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
while($row = mysql_fetch_array($result)){

  //start the session and register a variable

  session_start();
  session_register('username');

  //successful login code will go here...
	$log="You have successfully logged in!";

  }

  }
  else {

  //if nothing is returned by the query, unsuccessful login code goes here...

  $log="Incorrect login name or password. Please try again.";

  
  }
  ?>

<!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>
<title>Login :: P'burg Express Lines</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="content-style-type" content="text/css" />
<meta name="author" content="OhioPrepTalk.com" />
<meta name="copyright" content="Copyright (c) 2004, OhioPrepTalk.com" />
<link rel="STYLESHEET" href="css/style.css" type="text/css" />
</head>
<body>

<div id="body">

<div id="header">
<h1>P'burg Express Lines</h1>
</div> <!-- header -->

<div id="leftcontent">

<div id="menu">
<ul>
	<li><a href="index.html">company overview</a>
		<ul>
			<li>about our company</li>
		</ul>
	</li>
	<li><a href="login.html">monthly calendar</a>
		<ul>
			<li>calendar of transactions</li>
		</ul>
	</li>
	<li><a href="contact.html">contacting us</a>
		<ul>
			<li>how you can reach us</li>
		</ul>
	</li>
</ul>
</div> <!-- menu -->

<div id="announce">
<h3>announcements</h3>
<p>8/24 - website created</p>
</div> <!-- announce -->

</div> <!-- leftcontent -->

      <div class="contentblock"> 
        <h4>login</h4>
		 <?PHP
		 
		 echo $log;
		 ?>
			 
		 
     </div>
      <!-- contentblock -->
      

  <div id="footer"> 
    <p>Copyright &copy; 2004, <a href="mailto:[email protected]" title="Email Webmaster">A. Fred</a></p>
  </div>
</div> 
</div>
<!-- body -->

</body>
</html>

It says that there is an unexpected $ on line 129, which happens to be the last line of code. Any reason why on this one?

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 4 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

MySQL question

Hi OPTAdmin,
The only thing I can think is that the $log has no value.
Try setting $log=""; right at the top of the script.

Hope that helps

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Okay, so I tried that, and nothing happened. I got the same error, saying that I had an unexpected $ in the final line of code.

So, I tried to delete the row that I created for the second user, thinking that I did something wrong. Then I attempted to access the page again, unsuccessfully of course.

I'm so totally lost, haha.

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 4 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

MySQL question

Hi OPTAdmin,
You open an else statement near the top of the page that is never closed.
Try closing it after the </html> tag

</html> 
<?php 
} 
?>
Hope that helps

OPTAdmin
Offline
Enthusiast
Dover, OH -- Soon to be Columbus, OH
Last seen: 18 years 19 weeks ago
Dover, OH -- Soon to be Columbus, OH
Joined: 2004-07-27
Posts: 108
Points: 0

MySQL question

Okay, well I just restarted it all and it works now. *Sigh* Thanks, though!

I am going to be attending The Ohio State University this fall as an incoming freshman, majoring in Computer Science and Engineering.

Go Bucks!