Jul 25, 2008

Tricks With Php Variables

Free Web Hosting, No Ads > CONTRIBUTE > Tutorials
Pages: 1, 2

free web hosting

Tricks With Php Variables

coolcat50
You probably are thinking at this moment why I am posting some tricks with variables. Well, there are many useful things a variable can do for us. For one they can actually be used to create simple games. They also can be used to produce a random result in a page. Another great use is shoutboxes.

Well here are some cool variable effects.

Adding Multiple Strings
We can use strings to create great effects.
Example:
CODE
<?php
$var1="Hello person.";
$var2=str_replace("person","world",$var1);

echo $var1." ".$var2;
?>


We used str_replace to show two sentences with strings.
Neat huh.

Random Integers
This is one of the sweetest features of PHP. the rand() function. Here is a neat trick using this function.
Example
CODE
<?php
function exVar()
{
$num=rand(1,100);
if ($num<100)
{
echo "The number is less than 100.";
}
else
{
echo "The number is 100.";
}
}
exVar()
?>

That will run the function and tell you if it is 100 or less. Great for guessing games.

MySQL Connect
With variables we make connecting to a MySQL database clean and simple
Most mysql connection codes are like this.
CODE
<?php
$localhost="localhost";
$name="username";
$password="password";
$database="db";
$connect=mysql_connect($localhost,$name,$password) or die(mysql_error());
mysql_db_select($database,$connect);
?>


Simple huh! Variables make it look simpler and neater.

Superglobals
This is another awesome feature of PHP and variables. The $_POST and $_GET variables. We can use these to receive user input. We also can use the $_GET global to host a small website in ONE FILE.
$_POST is very widely used in registration scripts and chatboxes.
Example of $_GET
CODE
<?php
if ($_GET['var'] == 'var')
{
//Post some html/php stuff here
}
else
{
//post the main page code here
}
?>


Example of $_POST
CODE
<?php
$name=$_POST['var'];

echo $name;
?>
<form action="page.php" method="post">
<input type="text" name="var" />
<input type="submit" value="Submit" />
</form>


The first code gives us a one file site using the $_GET variable and If statements.

Our second code processes the form and prints the user input.

Thank you for reading my tutorial.

 

 

 


Reply

OneMinute
Nice one there. Would you mind having me on your MSN so that we can chat about PHP here? I need some training with PHP as i am beginner only. So as if i have any queries, perhaps you can help me?

Reply

wailedhero
Assalam-o-Alaikum!
(Peace be upon You)

Hey it was great! rolleyes.gif I am a newbie in PHP! PLease can you explain that how should we save it and this and that! Do you know how to make an upload script which can upload files to a database? PLease reply!

Reply

cwconline
Yeap another cool thing is this

CODE


<?php

$msg["welcome"] = "Welcome to our site";
$msg["GoodBye"] = "Take it easy... come back and see us";

echo $msg["welcome"];

?>



this is a good way to keep your message or your errors on a sepearte file to make it easy to call upon an error... although i did something completly diffrent than that above.

Reply

cwconline
QUOTE(wailedhero @ Jan 4 2008, 03:33 PM) *
Assalam-o-Alaikum!
(Peace be upon You)

Hey it was great! rolleyes.gif I am a newbie in PHP! PLease can you explain that how should we save it and this and that! Do you know how to make an upload script which can upload files to a database? PLease reply!



It's easy to save... just save it as a .php file

so in words

file.php


Reply

GaiaZone
The $_GET example isn't really clear... at all.

I'm not sure how to use it, so if anyone wouldn't mind explaining, I would greatly appreciate it. =]

Oh, I would also like to add that I read somewhere that some $_GET commands can be dangerous because people can inject Javascript snippets to steal cookies or bother people.

Reply

rvalkass
$_GET is used to get information out of the address, which is passed to the PHP script. For example, look at the address for this topic:

http://www.trap17.com/forums/index.php?showtopic=53286

The ? indicates that PHP variables will follow. showtopic is the name of the variable, the = indicates its value is next, and 53286 is the value for that variable.

You can access these variables with $_GET['variable_name']. So, in this example, in the PHP script you could use $_GET['showtopic'] to get the ID of a topic to look for. You could then use that value in a MySQL lookup to get the topic information.

QUOTE
Oh, I would also like to add that I read somewhere that some $_GET commands can be dangerous because people can inject Javascript snippets to steal cookies or bother people.


Yes. If you are not careful and have made glaring security mistakes with your code, then people can start inserting whatever code they like - thus allowing them to run JavaScript or other stuff. Generally, as long as you use the right functions to sanitise any inputs, you will be fine.

 

 

 


Reply

sonesay
This is one reason I try and not use any $_GET in my code. If I can I will always try and use $_POST so I wont have to deal with people trying to include their own code in there. I guess $_GET does have an advantage as it can be bookedmarked unlike $_POST. I think more experience will be able to tell you what you can and cant use. I probably will need to use $_GET sometime in the future but it hasnt come up just yet in my current project.

edit:

Thinking about it abit I cant see how anyone would be able to inject some javascripte code into the URL. I mean unless you parse out that varname+value pair in the URL in your code it would be outputed right? Unless I'm missing something else obvious here how can someone do that?

Reply

hitmanblood
Well honestly I have expected more from such name of tutorial however this is more or less just basic calculations with variables nothing more. I would also suggest everyone to check out sessions as this is one of the most usuful parts of php. And important thing to note is that when you are using any variable i the php you should also chec whether that variable is set due to the fact that php is not type strict language you must check whether variable is set and this must be obligatory job in the cases when you are passing one variable from one script to another.

Also I would like to say a bit more what does type strict means. (Just for the record I think about php very highly however this is one thing I hate it.)

Type stricts means that when you have some variable you must declare it with type for example int bool string as object char long double float and so on. However you do not have to do this in the php and because of that there are so many security issues and mistakes in the scripts that are really hard to trace.

So if you are writing larger scripts always check variables that are passed over and over because error might be just there, in most cases of my bugs I have discovered that it happened because certain variable had not been initialized.

------------------------------------------------------------------------------


Addition (simple code to check whether variable is set or not):

CODE
$var1 = 0;

if(isset($var1) && isset($var2)){

      echo "Nice $var1, $var2 <br>";

}else{

      echo "Variable not set <br>";

}


This code checks whether both varaibles are set. And if they are not and in this case $var2 is not set code witll return Variable not set and then new line.

---------------------------------------------------

This is another addition:

OK as I have read from one member that he mention java script injecions read more here it is asmall article about solution to certain javascript injection mission at hackthissite.org. So important thig is that basically with javascript you are merely trying to interface with html code that is you are chanigng html code in send to adopt it and use it to your cause. Java script injections are fairly dangerous and you should try to write neat code so that no script kiddie or someone else might change your scripts and force them to work in another manner then needed.

And one more important thing is that you can change php variables with javascript injections however it is not that usual because they have to be saved on your computer since php code is executed on the server side and not client side.

Reply

sonesay
Thanks for sharing the link hitmanblood. I had a quick read over it and I think I understand what you mean about being able to inject JS code in the url. So I guess if your trying to build a secure (as possible) site your should not rely on any HTML fields or javascript for any sensitive data as JS injection can be used to exploit it.

I've used hidden fields personally but thats just to store data to properly handle forms durings its life cycle. I think this time round I'ma think different when building my webpages.

Thanks again.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Pages: 1, 2
Recent Queries:-
  1. php mysqli_db_select - 35.97 hr back. (1)
  2. php url variables - 48.47 hr back. (1)
Similar Topics

Keywords : tricks, php, variables

  1. Ti-basic: Solving Equations With Variables
    Useful for Algebra (0)
  2. Second Php Tutorial - Variables
    (8)
    If you haven't read the first one, read it by clicking here . I got a lot of positive
    comments about my last tutorial. Do you guys think I should keep writitng these? Intro To PHP
    Tutorial 2 - Variables Released 4/06/07 By Chris Feilbach aka GhostRider Contact Info: E-mail:
    assembler7@gmail.com AIM: emptybinder78 Yahoo: drunkonmarshmellows Website:
    http://www.ghostrider.trap17.com It has been a very long time since I have released a tutorial.
    I've been quite busy working on my newest script for handling news (GAMES), and redesigning my
    webpage. This tu....
  3. Php Variables And Url Forwarding
    Tutorial -> PHP/URL forwarding (2)
    hi all this tutorial is about php get variable and url forwarding you can move variable in url with
    php with out html form make one page with this code : CODE <?php $what =
    $_GET['what']; $trap17 = $_POST['trap17']; echo
    "Your name is : " . "$what" . "and Your Trap username is : " .
    "$trap17"; ?> save this file with name "name.php" then upload name.php at
    your hosting , then turn your browser at this url : http://yoursite.trap17.com/name.php?what=....
  4. Firefox Tricks
    (32)
    Here are some tricks you can do to your firefox browser. Remove bold formatting on the active tab.
    Get rid of the close button the tab bar. Add the code to the userchrome.css CODE
    .tabs-closebutton-box { display: none !important; } Force firefox to load links ina
    new tab. Add the code to your user.js CODE user_pref
    ("browser.tabs.showSingleWindowModePrefs",true); Then restart firefox, you will
    see an option to open windows in a new tab instead under tabbed browsing in Tools|Options|Advanced
    Added code tags round cod....
  5. Saint-michaels Html Tips And Tricks #4
    been awhile so here somewhat new stuff (12)
    WEll its been awhile since i did so i will put up some more useful html tips and tricks here on
    trap17.com Ok for trick actually, want to know how some websites got those ool icons put on their
    website link in the favorites folder well here you go in 3 quick steps. 1. create an image then
    shrink it down to 16x16 and save it as avicon .bmp. 2. then change it to .ico, when is the file
    reconizing it as a icon., if you are having problems saving it, there are some resources out there
    that can change it for you. 3. load the image to your website and add this tag in betwee....
  6. Saint Michael's Dhtml Tips And Tricks Issue #2
    well i thought i catch up in my issues so heres some more (0)
    Todays is were are going to do some text effects that should be interesting for some people, the
    first one we are going to talk about is the matrix scrolling text effect, if you seen the moives and
    about 99% of you have then you know about hte computer screens now you can do it. this is done in a
    2 step process. STEP 1: Insert this script in the head tags QUOTE .matrix {
    font-family:Lucida Console, Courier, Monotype; font-size:10pt; text-align:center; width:10px;
    padding:0px; margin:0px;} <script type="text/javascript" language="JavaScript"> <!--....
  7. Saint-michael Html Tips And Tricks Issue #3
    exploring meta tags and what uses they have (0)
    Todays issue we are going to talk about a bunch of good newbie stuff. The first is meta tags to
    make it easy use a generator, from my searches on the internet ther are soe many meta tags to use
    its not even funny but for those whoe like them is are the more common ones that are used for a
    website. QUOTE also for a dynamic look using the meta this called page
    fading so to speak, what it does when you click on a link the website will fade for a duration of
    time to the next page on your site warning it doesn't do anything when you first cl....
  8. Saint Michael Xhtml Tips And Tricks #1
    xhtml= html 5.0 :P (3)
    Well by the many views i have seen on my web design issues i should go into new territory, so im
    going start with some useful xhtml info for you web design fenatics, the info i present to you is
    from other websites so be warned it might or might not work all depends but lets start off with the
    basic info for using xhtml. Also this tutorial is for beginners so i won't be doing any xml
    info due to it being a more server side then actually webdesign 1. DOCTYPE this is used to tell
    the browser what kind of infomation it will be displaying. nothing as really changed....
  9. Saint Michael Css2 Tips And Tricks Issue #1
    css tips and tricks for you beginners and experts (1)
    since my htmls tips and tricks issues are hot on the forum i decided to do some css stuff so lets
    get to work all you beginners and experts. Also note to those who read this is coming from other
    websites so i am not claming any rights to this info even though some people think if they make it,
    its theirs which is bs cuz if that were true then there would not be to many websites on the
    internet now would their?. Ok the scripting im using for right now is the the actually scripting
    on the website without external linking first css trick that is sometimes good to use is....
  10. Saint-michaels Html Tips And Tricks Issue # 2
    some more tricks of the trade and some wierd stuff to (2)
    Well i thought it was time to bring in issue 2 of html tips and tricks and i found a very
    interesting html coding which i thought was funny as hell its called Preventing Search Engine
    Indexing this coding is used to keep your website from being index why i don't know why you
    don't want you website listed unless it someone stupid as in telletubbies.com but heres the code
    This tag tells the robots not to index this page and not to follow any links within the
    page. QUOTE This tag tells the robots not to index this page, but follow any links wit....
  11. Saint_michael Html Tips & Tricks Issue #1
    useful html stuff (1)
    this little trick i though was useful it used to send a visitor to another page using meta tag and
    it sends the user to another page with in a few seconds for example you will go to the new page in 5
    seconds CODE <META HTTP-EQUIV=REFRESH
    CONTENT="5;URL=http://www.yoursitehere.com"> and for those have a good knowledge
    of javascript they could use this script right here CODE <html> <head>
    <title>Refresh Page</title> <script language="JavaScript">
    <!-- var time = null function move&#....

    1. Looking for tricks, php, variables

Searching Video's for tricks, php, variables
advertisement



Tricks With Php Variables



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE