Tag Archives: User (computing)

use php process a web form feedback–continue

since you got a form collect your information , so now you need create a database

here it is , you could save this as inc_connect.php

<?php
$db_name = “chinese_zodiac_database”;
$DBConnect = mysql_connect(“severname”,”usename”,”password”);
if (!$DBConnect)
{
echo “<p>unable to connect to the database server “.mysql_error().”</p>”;
}
else
{
$db = mysql_select_db($db_name);
if(!$db)
{
echo “<p>unable to connect to the database server “.mysql_error().”</p>”;
mysql_close();
$DBConnect = false;
}

}

?>

 

Using Basic Authentication

sometimes, when you get into a page then there a pop up window need your enter your name and password, here is the code, for authentication facilities are built into HTTP.

here is the code:

The user has not yet provided authentication information, it will be requested. If she has provided incorrect information, she is given a rejection message. If she provides a matching namepassword pair, she is presented with the contents of the page.

<?php

// if we are using IIS, we need to set
// $_SERVER[‘PHP_AUTH_USER’] and
// $_SERVER[‘PHP_AUTH_PW’]

if ((substr($_SERVER[‘SERVER_SOFTWARE’], 0, 9) == ‘Microsoft’) &&
(!isset($_SERVER[‘PHP_AUTH_USER’])) &&
(!isset($_SERVER[‘PHP_AUTH_PW’])) &&
(substr($_SERVER[‘HTTP_AUTHORIZATION’], 0, 6) == ‘Basic ‘)
) {

list($_SERVER[‘PHP_AUTH_USER’], $_SERVER[‘PHP_AUTH_PW’]) =
explode(‘:’, base64_decode(substr($_SERVER[‘HTTP_AUTHORIZATION’], 6)));
}

// Replace this if statement with a database query or similar
if (($_SERVER[‘PHP_AUTH_USER’] != ‘user’) ||
($_SERVER[‘PHP_AUTH_PW’] != ‘pass’)) {

// visitor has not yet given details, or their
// name and password combination are not correct

header(‘WWW-Authenticate: Basic realm=”Realm-Name”‘);

if (substr($_SERVER[‘SERVER_SOFTWARE’], 0, 9) == ‘Microsoft’) {
header(‘Status: 401 Unauthorized’);
} else {
header(‘HTTP/1.0 401 Unauthorized’);
}

echo “<h1>Go Away!</h1>
<p>You are not authorized to view this resource.</p>”;

} else {
// visitor has provided correct details
echo “<h1>Here it is!</h1>
<p>I bet you are glad you can see this secret page.</p>”;
}
?>

use php and mysql create a page need user name and password

so ,you already set up your databases(in your databases already  had member), there add more then your could let your member log in.

after the form the follow codes use this instead

<?php
} else {
// connect to mysql
$mysql = mysqli_connect(“localhost”, “webauth”, “webauth”);
if(!$mysql) {
echo “Cannot connect to database.”;
exit;
}
// select the appropriate database
$selected = mysqli_select_db($mysql, “auth”);
if(!$selected) {
echo “Cannot select database.”;
exit;
}

// query the database to see if there is a record which matches
$query = “select count(*) from authorized_users where
name = ‘”.$name.”‘ and
password = sha1(‘”.$password.”‘)”;

$result = mysqli_query($mysql, $query);
if(!$result) {
echo “Cannot run query.”;
exit;
}
$row = mysqli_fetch_row($result);
$count = $row[0];

if ($count > 0) {
// visitor’s name and password combination are correct
echo “<h1>Here it is!</h1>
<p>I bet you are glad you can see this secret page.</p>”;
} else {
// visitor’s name and password combination are not correct
echo “<h1>Go Away!</h1>
<p>You are not authorized to use this resource.</p>”;
}
}
?>

create database “auth” into mysql

here is the sourse for your databases

create database auth, there just create 2 users, you could create as many as you like, just insert the user name  and passwords. of course you also could create a page for page register into, following post i will write how to create a page insert data into mysql
use auth;
create table authorized_users ( name varchar(20),
password varchar(40),
primary key     (name)
);
insert into authorized_users values ( ‘username’,
‘password’ );

insert into authorized_users values ( ‘testuser’,
sha1(‘password’) );
grant select on auth.*
to ‘webauth’
identified by ‘webauth’;
flush privileges;