Test

Question 1

Write a program that is able to accept 2 inputs (provide textbox for key in input) which is the username (john) and password (abc123). When click login button, it heading to success web page which display the username key in by the user. Take note that in order to proceed to success web page require authentication of correct username and password, else it goes to fail web page. In addition the success web page is protected by the session.

Answer

1
2
3
4
5
6
7
<html>
<form action="action.php" method="POST">
username <input name="username" type="text"><br>
password <input name="password" type="password"><br>
<input type="submit" value="login">
</form>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

if($username == 'john' && $password == 'abc123')
{
session_start();
$_SESSION['id'] = $username;
header("Location: success.php");
}
else
header("Location: fail.php");
?>

Question 2

Modify/fix the code below in which when the VIEW hyperlink is click, it directs you to next web page (b.php) with this information (the given variable $a & $b) and the information is hold by the variables is able to be display on the other web page. You will need to add some line of code to display the values on the web page (b.php). Take note there is only TWO (2) web pages involve and the value of variable must be transmitted via the hyperlink.

1
2
3
4
5
<?php
$a = "john";
$b = "123";
echo <a href="b.php?a"=."$a"."\">VIEW</a>;
?>

Answer

1
2
3
4
5
<?php
$a = "john";
$b = "123";
echo '<a href="b.php?id1='.$a.'&id2='.$b.'">VIEW</a>';
?>
1
2
3
4
5
<?php
echo $_GET['id1'];
echo "<br>";
echo $_GET['id2'];
?>

Question 3

Write a program that able compute and display even number from 1-10, also don’t display 6 and 10. Each output must be displayed on new line. (Displaying string directly subject to even number is not allowed and the validation of even number should include 1 until 10)

Answer

1
2
3
4
5
6
7
<?php
for ($number = 1; $number <= 10; $number++)
{
if($number!=6 && $number!=10 && $number%2==0)
echo $number."<br>";
}
?>