School Map:  Home / Enroll $ Student Records; Class  
$ News @ LIVE Tech Radio * Support/FAQ | Store | FORUMS

Class / Forums / php101 Refresh the current page Week #5 Basic To-Do Assignment, post here
 

 (#8 2003-08-07 16:09:15) Post Reply
Page 0 1

TDavid
php mySQL Perl C/C++
Principal


Enrolled: Mar 2000
Posts: 193
AP: 1

Week #5 Basic TO-DO ASSIGNMENT: Write a script to submit a username and password to enter a page. If the username and password does not match the variable hardcoded in the script show the login form again with an error message. The password field should be private so that as you type you'll see ****. This should all be one self-contained script with NO include() or require() files.

Post this assignment here so that we can offer comment/critique for you, but also post a URL to the working copy of the script and post here the complete source code of the script in you post.

scriptname.php <-- script name

Good luck to you!

 

 (#65 2003-08-19 11:07:38) Post Reply
Page 0 1

Sk8rRIMuk
Need To Set


Enrolled: Aug 2003
Posts: 39
AP: 1

Here is the URL of my working script (use username Sk8rRIMuk and password havoctheman):
http://www.katikai.com/ee/assi...nt/username.php

Here is my script:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Week #5 Basic To-Do Assignment</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<center>
<?php
if($action=='true'){
if(
$username=='Sk8rRIMuk'&$password=='havoctheman'){
print(
"<p>Welcomehome$username,<br/>Clickthebelowlinkto
learnPHP:<br/><ahref="
http://www.scriptschool.com/php">Scripts School</a></p>
</center>
</
body>
</
html>");
exit;}
else {print('<p>Sorry the inputted Username and Password were incorrect please enter the correct username and password</p>');}
}
?>
<form method="
post" action="username.php">
<input type="
hidden" name="action" value="true" />
<input type="
text" value="Username" name="username" id="username" onfocus="this.value=''" /><br />
<input type="
password" value="Password" name="password" id="password" onfocus="this.value=''" /><br />
<input type="
submit" value="Submit" />
<input type="
reset" value="Reset"/>
</form>
</center>
</body>
</html>




That was amazingly simple yet it took me a while to figure utt hat I had spelt method & pasword wrong and that if you put spaces in the action's filename you get errors :S.

I will have a go ta the other one sometime this wekk, looks a little difficult though.

 

 (#154 2003-09-03 13:59:50) Post Reply
Page 0 1

Donnie
Need To Set


Enrolled: Aug 2003
Posts: 41
AP: 1

Somebody please help me out here. When I execute this script it allows me to enter the username and password, but it just returns to the same page asking for the username and password again. Any ideas?



<HTML>
<HEAD>
<TITLE>PHP Basic Assignment 5</TITLE>
</HEAD>
<BODY>
<?php
if ($action == "true"){
if (
$username=='phpstudent' & $password=='phptest'){
print(
"Username and Password Accepted");
exit;
}
else{
print(
"Username and Password are incorrect. Please enter correct values.");
}}
?>
<center>Please enter the username and password to continue:<br>
<b>Hint:</b><br> Username: phpstudent<br> Password:phptest
<form method = "post" action = "phpbasicassignment5.php">
<input type="hidden" name = "action" value = "true">
<input type="text" name="username"><br>
<input type="password" name="password"><br>
<input type="submit" value="submit">
</center>
</FORM>
</BODY>
</HTML>


 

 (#155 2003-09-03 14:12:33) Post Reply
Page 0 1

Sk8rRIMuk
Need To Set


Enrolled: Aug 2003
Posts: 39
AP: 1

This script works fine when it's uploaded to my site, my only quarrels are that you didn't print the </BODY></HTML> before you exited and yopu could use $PHP_SELF to identify the page you wan't to submit the form to.....

Are you sure you are typing them right, remember PHP is caps senstive...

 

 (#156 2003-09-03 15:09:55) Post Reply
Page 0 1

Donnie
Need To Set


Enrolled: Aug 2003
Posts: 41
AP: 1

Could you give me a little more information Sk8? I don't exactly understand what you mean about $PHP_SELF. Should I have put the html tags you mentioned before the exit?

 

 (#157 2003-09-03 18:07:33) Post Reply
Page 0 1

Sk8rRIMuk
Need To Set


Enrolled: Aug 2003
Posts: 39
AP: 1

It works fine as it is but I was offering pointers, try:

<HTML>
<HEAD>
<TITLE>PHP Basic Assignment 5</TITLE>
</HEAD>
<BODY>
<?php
if ($action == "true"){
if ($username=='phpstudent' & $password=='phptest'){
print("Username and Password Accepted\n</BODY>\n</HTML>");
exit;
}
else{
print("Username and Password are incorrect. Please enter correct values.");
}}
?>
<center>Please enter the username and password to continue:<br>
<b>Hint:</b><br> Username: phpstudent<br> Password:phptest
<form method = "post" action = "<?php $PHP_SELF ?>">
<input type="hidden" name = "action" value = "true">
<input type="text" name="username"><br>
<input type="password" name="password"><br>
<input type="submit" value="submit">
</center>
</FORM>
</BODY>
</HTML>

The PHP_SELF makes sure it submits it to itself and the </BODY></HTML> are there so the will be displayed before the exit and the \n make sure theres a newline in the source code...

It does work on my server thought I can only think of type errors in the login are the reason for it not working for you...

 

 (#174 2003-09-09 15:11:28) Post Reply
Page 0 1

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

no comments...



<html>
<body>
<?
$namehidden
= "Quick";
$passhidden = "saver";

if (
$action == "true") {
if (
$name == $namehidden && $pass == $passhidden) {
print(
"All that inserting data stuff for nothing... :P");
exit(); } else {
print(
"Login <b>name</b> and/or <b>pass</b> is/are incorrect, please try again.<P>"); } } ?>

<form method="post" action="<? $PHP_self ?>">
<input type="hidden" name="action" value="true">
Name: <input type="text" name="name" size=15><P>
Password: <input type="password" name="pass" size=15"><P>
<input type="submit" value="login">
</form>
</body>
</html>



working url

 

 (#200 2003-09-13 16:51:39) Post Reply
Page 0 1

operations
Need To Set


Enrolled: Sep 2003
Posts: 8
AP: 1

http://members.lycos.co.uk/operations/

Username : operations Password : 54321



<html>
<body>
<center>

<?php
/**
* TO-DO ASSIGNMENT: Write a script to submit a username and password to enter a page.
* If the username and password does not match the variable hardcoded in the script show
* the login form again with an error message. The password field should be private so
* that as you type you'll see ****. This should all be one self-contained script with NO *
* include() or require() files.
*/
$username = "operations";
$userpassword = "54321";

if (
$action == "true") {
if (
$name == "$username" &$password == "$userpassword") {
print(
"<title>Logged In</title>You have successfully Logged In</center></BODY></html>");
exit;
} else {
print(
"<title>Login Error</title><FONT COLOR=\"#FF0000\"><b>Wrong Username / Password</b></center></FONT</BODY></html>");
}
}

?>
<title>Script School Assignment 5 Basic</title>
<center>
<form method="POST" action="week5_basic_netsurprise.php">
<input type="hidden" name="action" value="true">
<input type="text" name="name" value="Username" size="10"> Username<br>
<input type="password" name="password" value="Password" size="10"> Password<br>
<input type="submit" value="Submit"> <input type="reset" value="Reset"></center>
</form></body></html>


 

 (#209 2003-09-15 10:53:12) Post Reply
Page 0 1

Shrek
Need To Set


Enrolled: Aug 2003
Posts: 33
AP: 1

I have some problems. I can't forward the username and password to the php file. I don't know what's wrong.

class05.php


<?

if($action=="true")
{
if(
$user=="jeffkwan" & $pass=="jeffkwan")
{
print(
"Login Successful!!!!");
exit;
}
else{
print(
"Username or Password Error!!!!");
exit;
}
exit;
}
else{
print(
"You can't access this page!!!!");
}
?>



Form Filling class05.htm

<FORM METHOD="POST" ACTION="class05.php">
<input type="hidden" name="action" value="true">
Login the PHP Week 5 Assignment<p>

Login Name:<br>
<input type="text" name="user" value="guest" size="20"><br>
Password:<br>
<input type="password" name="pass" value="guest" size="20"><br>


<input type="submit" value="Submit">


</FORM>



Could anyone help me to find it out?

 

 (#210 2003-09-15 10:54:18) Post Reply
Page 0 1

Shrek
Need To Set


Enrolled: Aug 2003
Posts: 33
AP: 1

When I run my php, it just shows
"You can't access this page!!!!"

Thx a lot

 

 (#211 2003-09-15 11:06:20) Post Reply
Page 0 1

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

you gotta put two && instead of just one i think, and you dont need two exits after the name and pass error, just one does the trick

other than that i see nothin wrong, although dont blame me if it still doesnt work, i gotta do some stuff on my pc before i do php again, probably in a couple of days or so

 

 (#212 2003-09-15 11:10:20) Post Reply
Page 0 1

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

oh, and dont put the "you cant access this page error", remove that else condition because, if you think well that else condition is always stated when the $action string is empty, and its logical that when you first run the script without filling in the form that string is empty so you made that error appear when the user didnt even do anything.

 

 (#213 2003-09-15 15:52:08) Post Reply
Page 0 1

Shrek
Need To Set


Enrolled: Aug 2003
Posts: 33
AP: 1

I have tried your suggestion.
1. Remove the else, and
2. Use && rather than &

Unfortunately, the string in all variables seem empty and the result returns to me is a blank page. The question in my heart is what can I bring the variables from form to php?

Thx a lot.

 

 (#214 2003-09-15 16:00:27) Post Reply
Page 0 1

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

huh??? i dont get it, have you tried taking the "you cant access..." error out? could you link me to somewhere where you may have uploaded your script?

 

 (#215 2003-09-15 16:17:37) Post Reply
Page 0 1

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

oh i think i got it, the name you give to each field of the form (for example "name") will become a string called $name when the form is submitted, with the value inserted at the form. but i dont get what you said bout the always getting the blank page error. you need to submit a link to the url with a "workin" (or not working so we can fix it) with the code you subbmit, that is also valid to the previous tasks youve done.

heh, im being technical here and i only have the eighth course with an error on the advanced task that i cant fix :P

oh by the way, i have nothing to do with it, you do as you like, but i noticed that you didnt make any advanced tasks till the moment, my advice: they really help a lot increasing your aknowledge on php as well as being logical with it. again i dont meen to offend or to command or anything, just a lil advice im givin out :)

 

 (#216 2003-09-16 05:40:25) Post Reply
Page 0 1

Shrek
Need To Set


Enrolled: Aug 2003
Posts: 33
AP: 1

Thank you for your help. I have found a web hosting to hosting my class file.
http://members.lycos.co.uk/jef...wan/class05.htm

The file works well there. May be my php server got something wrong.

BTW, I will try my best to do the advanced assignment. Thank you very much.

 

 (#217 2003-09-16 05:56:35) Post Reply
Page 0 1

Shrek
Need To Set


Enrolled: Aug 2003
Posts: 33
AP: 1

I finally find out the result. I need to amend the class05.php.



<?
//$_REQUEST["action"]
$action=$_REQUEST['action'];
$user=$_REQUEST['user'];
$pass=$_REQUEST['pass'];

if(
$action=="true")
{
if(
$user=="jeffkwan" && $pass=="jeffkwan")
{
print(
"Login Successful!!!!");
exit;
}
else{
print(
"Username or Password Error!!!!");
exit;
}
exit;
}
?>




I found that the php4 cannot directly get the variables from the html. http://hk.php.net/variables.external

 

 (#284 2003-09-26 18:35:08) Post Reply
Page 0 1

maddy
Need To Set


Enrolled: Sep 2003
Posts: 14
AP: 1

I did two variatons for this assignment - the first just prints out a message if the username/password combo is correct (example here: http://sassy.togetherhost.com/...pts/week5.php). The second redirects to another page if it is correct (example here: http://sassy.togetherhost.com/.../password.php).


<html>
<head>
<body>
<?php
if($action=='true'){
if(
$username=='maddy' & $password=='rocks'){
print(
"Congratulations, you know who you are!</body></html>");
exit;}

else {print(
'Sorry, you seem to be confused, please try again.');}
}
?>
<h4>Please enter your details<h4>
<form method="post" action="week5.php">
<input type="hidden" name="action" value="true" />
Username:<input type="text" name="username" id="username"/><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="enter" />
</form>
</body>
</html>



<?
if ($action == "true") {
if (
$username=='maddy' & $password=='rocks'){
header("Location: <A HREF="http://sassy.togetherhost.com/scripts/secret_page.php");" TARGET=_blank>http://sassy.togetherhost.com/...et_page.php");</A>
exit;
}

else {
print(
"You are not allowed to enter. Try again.");}
}
?>
<html>
<head>
<title>Password Entry</title>
</head>
<body>
<h4>Type your details to enter:</h4><br />
<form method = "post" action = "password.php">
<input type="hidden" name = "action" value = "true" />
Username:<input type="text" name="username" /><br />
Password:<input type="password" name="password" /><br />
<input type="submit" value="submit" />

</form>
</body>
</html>


I guess the second is jumping ahead to week 8,but I figure I'm most likely to redirect if a password is correct, as compared to printing out a message. I found this helpful: http://nz2.php.net/manual/en/f...tion.header.php
:)

 

 (#285 2003-09-26 18:39:55) Post Reply
Page 0 1

maddy
Need To Set


Enrolled: Sep 2003
Posts: 14
AP: 1

Grr. The forum attached my closing brackets to my URLS. I'll post them again in a second. Anyway, I forgot to note, although it's obvious from the code, that the username is 'maddy' and the password 'rocks'. :)

First example: http://sassy.togetherhost.com/...ripts/week5.php
Second example (with redirect): http://sassy.togetherhost.com/...ts/password.php

It'd be neat if we could edit out posts here.

 

 (#286 2003-09-26 18:43:02) Post Reply
Page 0 1

maddy
Need To Set


Enrolled: Sep 2003
Posts: 14
AP: 1

Hey, what's happened to my code? The php bit of the second bit I posted is way different to what I posted. I'll try again (just the php bit)

<?
if ($action == "true") {
if (
$username=='maddy' & $password=='rocks'){
header("Location: <A HREF="http://sassy.togetherhost.com/scripts/secret_page.php");" TARGET=_blank>http://sassy.togetherhost.com/...et_page.php");</A>
exit;
}




else {
print("You are not allowed to enter. Try again.");}
}
?>

 

 (#287 2003-09-26 18:45:09) Post Reply
Page 0 1

maddy
Need To Set


Enrolled: Sep 2003
Posts: 14
AP: 1

Still not working!!! I'll just link to the file instead: http://sassy.togetherhost.com/...ts/password.txt

I know this is free, but maybe a beter forum software is needed - Invision Power Board is great (and free). :)

 

 (#353 2003-11-02 12:44:26) Post Reply
Page 0 1

Dest
Need To Set


Enrolled: Oct 2003
Posts: 18
AP: 1

I was going to make advanced assignment but i decided to do this first. i used include command, but not for main script. just made variation, and after successful login you are taken in to my pic of the day script :)

Name: dest
Password: script

Working example: http://tts.lt/~sir/scripts/week5basic.php



<html>
<title> Week 5 basic to-do assignment </title>
<body>
<center>
<?

if($form!="done") { print("Welcome to my NeoPet of the day system. Please log-in:"); }

//Set variables
$loginreal="dest";
$passreal="script";

if(
$form=="done")
{
if(
$login==$loginreal&&$pass==$passreal) {print("You successfuly logged in and you can now see our NeoPet of the day:)<br><br>");
include(
"week3.php"); exit; }
else {print(
"<font color=red>Your password or login is incorrect.</font>");}
}
?>



<form method=POST action=week5basic.php>
<input type=hidden name=form value=done>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Login:<input type=text name=login size=15>
<br>Password:<input type=password name=pass size=15>
<br><br><input type=submit value=Submit>&nbsp;&nbsp;<input type=reset value=Clear>

</form>
</center>
</body>
</html>


 

 (#375 2003-11-14 11:02:36) Post Reply
Page 0 1

zxrod
Need To Set


Enrolled: Oct 2003
Posts: 7
AP: 1

Here's my basic assignment. It seemed fairly easy to me..



<html>
<body>
<?
$user
="zxrod";
$password="scriptschool";
if(
$action=="true") {
if(
$username == $user && $pass == $password) {
print(
"Thank you for logging in.. champ!");
exit();
}
else {
print(
"I'm sorry, you do not have permission to view this website");
}
}
?>

<form method="post" action="week5_basic.php">
<input type="hidden" name="action" value="true">
Banner Account Login:<p>
Username:<br>
<input type="text" name="username"><p>
Password:<br>
<input type="password" name="pass"><p>
<input type="Submit" value="Submit">
</form>

</body>
</html>


 

 (#417 2003-12-12 06:28:54) Post Reply
Page 0 1

fudded
Need To Set


Enrolled: Dec 2003
Posts: 5
AP: 1

I have a problem here, no matter what I do I get this error

Undefined variable: action in c:\inetpub\wwwroot\learn php\TMPh21ypuns2.php on line 14
I have even borrowed Quicksaver code to check i get the same "variable: action .....". I guess its this line of code



if ($action == "true") {



Any help would be helpful

Thanx

Sean

 

 (#419 2003-12-18 06:09:19) Post Reply
Page 0 1

Quicksaver
Need To Set


Enrolled: Sep 2003
Posts: 90
AP: 1

post the previous lines before that chunk of code, sometimes its something behind and not exactly that one

 

 (#441 2003-12-30 01:24:02) Post Reply
Page 0 1

PureGrain
Need To Set


Enrolled: Nov 2003
Posts: 14
AP: 1

OK, this is getting me frustrated. I cannot seem to get it to work on my server. I am hosting my own of coarse and seem to get most every other weeks assignments to work, with the exception of week 3's advanced assignment, which I posted for help on in the advanced forum. But here is the code i revamped to fit others once I could not get it to work the way I tried it. Which now makes me think it might have worked if my server allowed it too. I am not sure if that is my problem or am I missing something?


<html>
<head>
<title>Lesson 5 Basic</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body><p align="center">(basic) TO-DO ASSIGNMENT: Write a script to submit a username and password<br>
to enter a page. If the username and password does not match the variable hardcoded in the script <br>
show the login form again with an error message. The password field should be private so <br>
that as you type you'll see ****. This should all be one self-contained script with NO include() or require() files. </p>
<p align="center">Code begins below this line.</p>
<hr>
<p align="center">&nbsp; </p>
<?php
//This has been confusing as hell
$username=("puregrain");
$password=("open");
if (
$action == "true")
{
if (
$user == $username && $pass == $password)
{
print(
"<p>Thanks for signing in to this site!");
exit();
}
else
{
print(
"Sorry, the information you provided is incorrect.");
}}
?>

<form method="post" action="lesson5_basic.php">
<input type="hidden" name="action" value"true">
<input type"text" name="username">Insert Username<br>
<input type="password" name="password">Insert Password<br>
<input type="Submit" name="submit" value="Login">
</form>
</body>
</html>


 

 (#442 2003-12-31 01:17:09) Post Reply
Page 0 1

n2o2k
Need To Set


Enrolled: Dec 2003
Posts: 6
AP: 1

Here is the basic assignement and a link to an example: http://membres.lycos.fr/justry...school/course_5



<?
/****************************************************
* To do Assignement #5 *
* by n2o_2k *
* n2o_2k@hotmail.com *
* *
****************************************************/


?>
<html>
<HEAD>
<TITLE>SchoolScript.com Assignment #5 - URL of the Day</TITLE>
</HEAD>
<BODY>
<?
/*Basic:Write a script to submit a username and password to enter a page.
*If the username and password does not match the variable hardcoded in the
* script show the login form again with an error message. The password field
* should be private so that as you type you'll see ****. This should all be
* one self-contained script with NO include() or require() files.
* */
$true_username="n2o_2k";
$true_password="lifesucks";

//check the value of the variable action
if (isset($action))
{
if (
$action=="true")
{
//true when user try to login

//check the username and password
if (($true_password==$password) && ($true_username=$username))
{
//welcome the user login
print("<h1>Login correct</h1><br>Welcome $username");
exit;
}else
print(
"Incorrect username and password <br>Please try again..<br>");
}
}
?>
<center>
<form method="post" action="index.php" >
<input type="hidden" name="action" value="true">
<h1>Please login</h1><p>

<table>
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan=2>
<input type="submit"><input type="reset">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>


 

 (#447 2003-12-31 18:24:47) Post Reply
Page 0 1

PureGrain
Need To Set


Enrolled: Nov 2003
Posts: 14
AP: 1

I fixed it. :D
I had left an = off in the form..


<html>
<head>
<title>Lesson 5 Basic</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
//This has been confusing as hell
$username = "puregrain";
$password = "open";

if (
$action=="true"){
if (
$name == $username && $pass == $password){
print(
"Welcome <b>$username</b>. Thanks for signing in to this site!");
exit();}else{
print(
"Sorry, the information you provided is incorrect.");
}}
?>
<h4>Plese enter your username and password below:</h4><br>
<p><b>Hint:</b><br><b>User:</b>puregrain<br><b>Password:</b>open</p>
<form method="post" action="<? $PHP_SELF ?>">
<input type="hidden" name="action" value="true">
<input type="text" name="name" size="15"><br>
<input type="password" name="pass" size="15">
<input type="submit" value="login">
</form>
</body>
</html>



working url: http://members.lycos.co.uk/xko...sson5_basic.php

 

 (#544 2004-03-09 23:14:54) Post Reply
Page 0 1

Havoc
Need To Set


Enrolled: Mar 2004
Posts: 3
AP: 1

This one was pretty easy, But how do you get multiple usernames and passwords with different pages for each one?


<html>
<body>
<?
$namehidden
= "Havoc";
$passhidden = "xgirocks";

if (
$action == "true") {
if (
$name == $namehidden && $pass == $passhidden) {
print(
"Login successful.");
exit(); } else {
print(
"Login unsuccessful. Please try again.<P>"); } } ?>

<form method="post" action="<? $PHP_self ?>">
<input type="hidden" name="action" value="true">
Name: <input type="text" name="name" size=15><P>
Password: <input type="password" name="pass" size=15"><P>
<input type="submit" value="login">
</form>
</body>
</html>


 

 (#547 2004-03-10 14:48:45) Post Reply
Page 0 1

Cyb
Need To Set


Enrolled: Feb 2004
Posts: 6
AP: 1

Well there are more ways to do that... one would be starting with another if (password == other pass and username == otherusername).
You can primarely only send everyone entering the form to the same place (I think), so I would then redirect them with a variable header location to another page which you will learn in course 10 :).

 

  Page 0 1

View Previous Thread
Print this page Print This Page

View Next Thread

School Map:  Home / Enroll $ Student Records; Class  
$ News @ LIVE Tech Radio * Support/FAQ | Store

Advertising | Link To Us | Privacy 
Copyright 2000-2002 Script School Productions / KMR Enterprises 
No part of this website may be reproduced, copied and/or distributed in any medium 
without express written permission