I am working on a password protected website that will have multiple users. I'll be giving each user their own username and password (they won't be creating them or registering or anything). Once they sign in, they will be redirected to a page that will say, "Welcome, Mr. Smith!" I also want to have more information in the database about them, like their first name, their spouse's name, etc.) I'm trying to us MySQL and php and I think I'm really close to figuring out how to do this, but I'm getting the error:
Failed to connect to server: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Here is the code I'm using:
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(host, user, pass);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(database);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$login = clean($_POST['login']);
$password = clean($_POST['password']);
//Input Validations
if($login == '') {
$errmsg_arr[] = 'Login ID missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login-form.php");
exit();
}
//Create query
$qry="SELECT * FROM table WHERE user='$login' AND pass='".md5($_POST['password'])."'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_USER'] = $user['user'];
$_SESSION['SESS_FULL_NAMES'] = $user['full_names'];
$_SESSION['SESS_GUES_ONE'] = $user['guest_one'];
session_write_close();
header("location: member-index.php");
exit();
}else {
//Login failed
header("location: login-failed.php");
exit();
}
}else {
die("Query failed");
}
?>I've double-checked the host, user, and pass to make sure they are right and they are. I also have a mysql.sql text document that has all the information that I want about each user in it. I've put that in my root folder along with everything, but I'm not really sure where that falls in the mix of things... how does the file pull that information from it? Here is what is in the text doc:
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`user` varchar(10) collate latin1_general_ci NOT NULL default '',
`pass` varchar(10) collate latin1_general_ci NOT NULL default '',
`full_names` varchar(40) collate latin1_general_ci default NULL,
`guest_one` varchar(20) collate latin1_general_ci default NULL,
`guest_two` varchar(20) collate latin1_general_ci default NULL,
`guest_three` varchar(20) collate latin1_general_ci default NULL,
`guest_four` varchar(20) collate latin1_general_ci default NULL,
`guest_five` varchar(20) collate latin1_general_ci default NULL,
`guest_six` varchar(20) collate latin1_general_ci default NULL,
PRIMARY KEY (`user`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`user`, `pass`, `full_names`, `guest_one`, `guest_two`) VALUES("john", "password", "Mr. and Mrs. John Smith & Family", "John Smith", "Jane Smith");
Thank you for any help in advance!