Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-18-2007, 11:48 AM
Senior Member
 
Join Date: May 2007
Posts: 181
Blog Entries: 1
skyfe is on a distinguished road
[PHP part 1]use of if/else loops & forms CHAPTER 1

Hello,
today I'm going to teach about php loops
Everything after // are only comments.
Oke, let's start with the if loop!

An if loop works like this:

Quote:
if( condition is true ) {
do this...
}
an example:

Quote:
if($name == 'skyfe') {
echo "You're god!";
}
Oke, this will check the variable $name, if the var name is equal to 'skyfe', THEN do: echo "You're god!";

Oke, now we know how to check only ONE thing, but if we would like to check more things we'll be using the elseif()
It will first check if the condition in the if loop is true, if not: it will check if the condition in the elseif is true.

an example:

Quote:
if($name == 'skyfe') { //if $name is equal to skyfe
echo "Welcome god skyfe!"; //echo this
}elseif($name == 'peter') { //if $name is equal to peter
echo "Hello Peter!";
}
This script checks the value of the variable $name, if it's equal to 'skyfe' it will say: Welcome god skyfe!
But if it's equal to peter, it will say: Hello Peter!";
We can also use this for a little security system.
Example: You want to make a security login which only let you acces to a page when you login with a username (only ONE is correct) and a password.
Then we can use this with a form:

file: login.php
Quote:
<form method='POST' action='index.php'> //values which will be put in the fields can be used with the POST method, and when the form will be submited you'll be redirected to index.php

username: <input type='text' name='username'> //lets the user fill in a value for the var username
<br>
password: <input type='password' name='password'> //let the user fill in a value for the var password
<br>
<input type='submit' value='login' name='submit'> //defines a submit button with as value 'login', we called the button 'submit', so we can later check if it's pressed
</form> //end form
Oke, now we have the html form part, now we want to check if the submit button is pressed. We'll be going to use a if loop for this of course. All form variables will be set into a $_POST['var_name'] so take for example the submit button, we called it submit, so it can be called by: $_POST['submit']
Now we want to check if it's pressed. We'll just check if the var $_POST['submit'] exist (this will be the case if it's pressed, caus then it will be POSTED into the $_POST[' ']) we'll do this with the 'isset()' method, isset checks if it exist.
Example:

Quote:
if(isset($_POST['submit'])) { //if the submit button is set in $_POST['submit'] (if that exist and the button is pressed so) then
do this;
}
If it's pressed, we want to check if the username and password are correct. lets take for example: we use as username "skyfe" and as password "god"
Then we should have to check if $_POST['username'] is equal to skyfe, and $_POST['password'] is equal to god, caus all variables will be set in a $_POST[' '] once the form is submited, remember?
We'll do this like this way:

Quote:
if($_POST['username'] == 'skyfe' && $_POST['password'] == 'god') // if the username filled in the field called 'username' is equal to skyfe AND the password filled in the field called 'password' is equal to god, THEN do the follow
...
}
Oke, now we know how to check if the form is submited AND if the username and password filled in are correct. Let's put it all together:

Quote:
<?php
if(isset($_POST['submit'])) { //if the button called 'submit' is pressed(and exist)
if($_POST['username'] == 'skyfe' && $_POST['password'] == 'god') // checks if the username filled in is equal to skyfe and password filled in is equal to god
echo "Welcome administrator!"; //then it will put out "welcome administrator" and you can put here the page where only admins (which should know the password and username) may access to
}else{ // else, if it's not true and the password is not equal to god or/and the username is not equal to skyfe
echo "Sorry, you don't have access to this page...";
}
}else{ //if the $_POST['submit'] var doesn't exist and the button is not pressed yet and the form not submited yet, then show the form
?>
<form method='POST' action='index.php'> //values which will be put in the fields can be used with the POST method, and when the form will be submited you'll be redirected to index.php

username: <input type='text' name='username'> //lets the user fill in a value for the var username
<br>
password: <input type='password' name='password'> //let the user fill in a value for the var password
<br>
<input type='submit' value='login' name='submit'> //defines a submit button with as value 'login', we called the button 'submit', so we can later check if it's pressed
</form> //end form
<?php
} //end the else
?>
Cool! Now we have our own security system! It wasn't that hard, was it?
Cya in my next tutorial
Hope you could understand my tutorial and sorry for my english, it isn't that good lol... But hope it was understandable
Greeetzzz.

Reply With Quote
  #2 (permalink)  
Old 05-24-2007, 09:35 PM
Junior Member
 
Join Date: Mar 2007
Posts: 22
Puffi is on a distinguished road
Nice Work very good

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 12:27 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com