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>”;
}
?>

Advertisements

In the databases sourse: “shal(‘password’)” is strongest way protect your password.

Regardless of whether you store your data in a database or a file, storing the passwords as
plain text is an unnecessary risk.A one-way hashing algorithm can provide better security
with very little extra effort.
PHP provides a number of one-way hash functions.The oldest and least secure is the
Unix Crypt algorithm, provided by the function crypt().The Message Digest 5 (MD5)
algorithm, implemented in the function md5(), is stronger.

Stronger yet is the Secure Hash Algorithm 1 (SHA-1.) The PHP function sha1()
provides a strong, one-way cryptographic hash function.

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;

Create a php page that will process the order using an array and display the results on another page.

your html form must look like blown:
———————————————————————————————————————————

<form action=”hardware_order.php” method=”post”>
<fieldset>
<legend>Order Form</legend>
<p><label for=”user”> Name</label> <input type=”text” name=”name” id=”user” /></p>
<p><strong>Select Some Products:</strong> <br>
<select name=”products[]” multiple>
<option value=”Sonic Screwdriver”>Sonic Screwdriver</option>
<option value=”Hammer”>Hammer</option>
<option value=”Drill”>Drill</option>
<option value=”Spanner”>Spanner</option>
</select>
<p><input type=”submit” name=”submit” value=”send”></p>
</fieldset>
</form>

————————————————————————————————————————
there are two ways can proceed this: one is :
————————————————————————————————————————-
<?php
$name =trim($_POST[‘name’]);
if(empty($_POST[‘products’])||empty($name))
{
echo “nothing has been selected”;
}
else
{
echo “Thanks “.$name.” your select products is “;

foreach($_POST[‘products’] as $o)
{
echo $o.”<br/>”;
}
}
?>
————————————————————————————————————————–
another ways is teacher’s answers:
———————————————————————————————————————————<?php
$name =trim($_POST[‘name’]);
if (!empty($name))
{     echo “<p>Welcome <b>$name</b>!</p>”;
}
else
{
echo “<p>Enter a name</p>”;
}

if(!empty($_POST[‘products’]))
{
echo “<ul>”;
echo “<p>Your product choices are:<br/>”;
foreach ($_POST[‘products’] as $value)
{
echo “<li>$value</li>”;
}
echo “</ul>”;
}
else
{
echo “<p> Nothing selected</p>”;
}
?>

 

The Chinese Zodiac | a code demonstration for PHP

The Chinese Zodiac | a code demonstration for PHP

Chinese zodiac signs represent twelve different types of personalities. Traditionally beginning with the sign of the rat. Though the animal assigned for the birth year is not wholly who a person is, as there is also an animal assigned by the birth month called inner animals and by the birth hour called secret animals. These with a combination of five elements, make up 8,640 possible combinations for a persons personality trait, and all are considered important for the proper use of chinese astrology.

There are many versions of the story about the beginning of the zodiac, one such story begins with the rat being given the task on inciting the animals to report to the Jade Emperor to be selected for the zodiac. The cat being one of the animals selected was a good friend of the rat who tricked him into believing the banquet was a day later and the cat slept through it thinking it was not till the next day. When the cat found out he vowed to be the rats’ swore enemy for the ages.

use file store infomation to string

$outputstring = $date.”\t”.$tireqty.”\t”.$oilqty.” oil\t”.$sparkty.” spark plugs\t\$”.$totalamount.”\t”.$address.”\n”;

@$fp = fopen(“DOCUMENT_ROOT/../orders/orders.txt”,’ab’);

flock($fp,LOCK_EX);
if(!$fp){
echo “<p><strong>Your order could not be process at this time. Please try again later.</strong></p></body></html>”;
exit;
}
fwrite($fp, $outputstring,strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);

echo “<p>Order written.</p>”;

Loading Arrays from Files (use for loop)

<?php
$DOCUMENT_ROOT = $_SERVER[‘DOCUMENT_ROOT’];
$orders=file(“DOCUMENT_ROOT/../orders/orders.txt”);
$number_of_orders = count($orders);
if ($number_of_orders == 0){
echo ”

No orders pending.Please try again later.

“;

}else{

for ($i=0;$i<$number_of_orders;$i++){

echo $orders[$i].”
“;
}

}
?>

3 dimensional arrays with for loop

<?php
    $categories = array(array( array( ‘CAR_TIR’, ‘Tires’, 100 ),
                        array( ‘CAR_OIL’, ‘Oil’, 10 ),
                        array( ‘CAR_SPK’, ‘Spark Plugs’, 4 ) ),
                 array( array( ‘VAN_TIR’, ‘Tires’, 120 ),
                        array( ‘VAN_OIL’, ‘Oil’, 12 ),
                        array( ‘VAN_SPK’, ‘Spark Plugs’, 5 ) ),
                array( array( ‘TRK_TIR’, ‘Tires’, 150 ),
                        array( ‘TRK_OIL’, ‘Oil’, 15 ),
                        array( ‘TRK_SPK’, ‘Spark Plugs’, 6 ) )
                        );
    for ($layer =0;$layer <3; $layer++){
        echo “layer $layer<br/>”;
        for ($row =0; $row<3; $row++){
            for ($column =0; $column<3; $column++){
            echo ‘|’.$categories[$layer][$row][$column];
        
        }
            echo ‘|<br/>’;
        }
    
    }
    
    
    
    
?>

Full stack developer