Jump to content

Customer Login System


ne014x

Recommended Posts

I have the pressure of learning PHP on my back to build a customer login system for 3 diffrent clients who are requesting it or they will close their accts.

 

I got my hands on some 3rd party code but can't seem to get it to work,  My first problem is that the script can't connect to the MYSQL PHP admin Database

 

I created the database and table for it using

CREATE TABLE users (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(60), password VARCHAR(60))

 

 

Everytime I load the page I get:

Can't connect to MySQL server on 'php.zygatetech.com' (61)

 

Keep in mind this is my first exsperince with PHP and I am still learning the basics

 

The code is:

<?php 
// Connects to your Database 
mysql_connect("php.zygatetech.com", "replaced with username", "replaced with password") or die(mysql_error()); 
mysql_select_db("data") or die(mysql_error()); 

//This code runs if the form has been submitted
if (isset($_POST['submit'])) { 

//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'") 
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.');
}

// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}

// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}

// now we insert it into the database
$insert = "INSERT INTO users (username, password)
VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$add_member = mysql_query($insert);
?>


<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.</p>
else 
{ 

// if login is ok then we add a cookie 
$_POST['username'] = stripslashes($_POST['username']); 
$hour = time() + 3600; 
setcookie(ID_my_site, $_POST['username'], $hour); 
setcookie(Key_my_site, $_POST['pass'], $hour);	

//then redirect them to the members area 
header("Location: members.php"); 
} 
} 
} 
else 
{	

// if they are not logged in 
?> 
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
<table border="0"> 
<tr><td colspan=2><h1>Login</h1></td></tr> 
<tr><td>Username:</td><td> 
<input type="text" name="username" maxlength="40">
</td></tr> 
<tr><td>Password:</td><td> 
<input type="password" name="pass" maxlength="50"> 
</td></tr> 
<tr><td colspan="2" align="right"> 
<input type="submit" name="submit" value="Login"> 
</td></tr> 
</table> 
</form> 
<?php 
} 

?>

Link to comment
Share on other sites

try useing some software to help you learn abit of mysql

i use 'TOAD' Google it.

 

ITs free and has several forms for you to fill in, its then creates some Mysql Code, Shows you it and excutes it.

 

If it doesnt work it will tell you in simple english (No error codes that are very long and strange) what is wrong and what you need to do.

 

Also just check if your MySql Database is open to 'Remote Database Access'!

 

Hope i helped in a way

Link to comment
Share on other sites

The host name needs to be the host of the MySQL database server.  You should first try using 'localhost.'

 

A couple of other things jump out immediately:

 

1. In your first if-conditional, you use the bitwise OR operator |.  You should be using the logical OR operator ||.

 

2. You need to add <?php before the else-statement that sets the cookie.

Link to comment
Share on other sites

 

Also just check if your MySql Database is open to 'Remote Database Access'!

 

 

Do you know where at this would be?

 

Trying to connect with localhost I get:

 

Can't connect to local MySQL server through socket '/tmp/mysql.sock' (46)

Link to comment
Share on other sites

What you specify for the hostname/IP in the mysql_connect() is dependent on and is specific to each hosting provider.

 

Both of the errors so far - Can't connect to MySQL server ... mean that you need to find out from the hosting provider how to specify the mysql server for that account (different accounts at the same web host can use different mysql servers.)

 

The page in the control panel where you created the database and the database user/password often displays the mysql server hostname or IP information that you need for the mysql_connect() statement.

Link to comment
Share on other sites

What you specify for the hostname/IP in the mysql_connect() is dependent on and is specific to each hosting provider.

 

Both of the errors so far - Can't connect to MySQL server ... mean that you need to find out from the hosting provider how to specify the mysql server for that account (different accounts at the same web host can use different mysql servers.)

 

The page in the control panel where you created the database and the database user/password often displays the mysql server hostname or IP information that you need for the mysql_connect() statement.

 

You were right Yahoo uses "mysql" as the host name, but now all I get is a blank page so Im off to troubleshoot some more

Link to comment
Share on other sites

Ok, so I got the register log in and members area page working but how do I determine content for each each user, say I want the user user1 but not user2 to see an invoice how would I do that?

 

Members Area:

<?php
// Connects to your Database
mysql_connect("mysql", "******", "******") or die(mysql_error());
mysql_select_db("data") or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}

//otherwise they are shown the admin area
else
{
echo "Admin Area<p>";
echo "Your Content<p>";
echo "<a href=logout.php>Logout</a>";
}
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}
?> 

 

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.