Jump to content


Photo

strange problem with variables


  • Please log in to reply
5 replies to this topic

#1 freenity

freenity
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 10 August 2006 - 07:13 PM

Hi
Today Ive instaled apache 2.2 with php 5 on my machine and have some strange problem. This is the code:

<?php


if (isset($ok))
echo "hi";
else
echo "bye";

?>

and it always prints "bye" even if I enter like this: localhost/file.php?ok=asd
it seems that it doesnt detect that variable was set. Might some problem with configuration. Thanks for any help.


#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 10 August 2006 - 07:16 PM

<?php


if (isset($ok)) {
   echo "hi";
}else {
   echo "bye";
}
?>
You have to figure out how to format correctly first.  Passing variables have to come from another page.  You have to have a page with a link or something to pass the variables.  I guess you MIGHT be able to type it for the same page, and hit enter to reload the page.  It would pass a get variable to itself, but even then you would have to do
if (isset($_GET['ok'])) {
instead of what you have.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 freenity

freenity
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 10 August 2006 - 07:22 PM

<?php


if (isset($ok)) {
   echo "hi";
}else {
   echo "bye";
}
?>
You have to figure out how to format correctly first.  Passing variables have to come from another page.  You have to have a page with a link or something to pass the variables.  I guess you MIGHT be able to type it for the same page, and hit enter to reload the page.  It would pass a get variable to itself, but even then you would have to do
if (isset($_GET['ok'])) {
instead of what you have.


wow it works. thanks a lot.
I am a bit confused because I ve tested the script I had posted before on another server and it worked just fine.

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 10 August 2006 - 07:24 PM

basically, you don't have register_globals turned on. that's a good thing. something to consider in addition to what businessman recommended: if you're ever going to have any other way to assign a value to $ok before your if statement, you may want to do something like this to assign a default value to it right off the bat:
<?php
$ok = isset($_GET['ok']) ? $_GET['ok'] : '';

if (!empty($ok)) echo "hi!";
else echo "bye!";
?>

so, on the server that it worked on, you must have had register_globals on.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 10 August 2006 - 07:28 PM

PHP 5 has a setting called Register Globals disabled by default.

This setting, when enabled, allowed values passed through a URL and form to be assigned directly to a variable of the same name, which was a security issue.

With the setting disabled, you have to access the variable from it's SuperGlobal array like businessman332211 mentioned.

Values passed through a URL will be assigned in the $_GET array:

// Example URL: "http://www.mysite.com/index.php?var=foo
$var = $_GET['var']; // $var would equal "foo"

Values passed through an HTML form will be assigned in either the $_GET or $_POST array, depending on the form method used:

/* Example HTML POST form
<form action="index.php" method="POST">
<input type="text" name="var" value="foo">
<input type="submit" name="submit" value="Submit">
</form>
*/
$var = $_POST['var']; // $var equals "foo"


/* Example HTML GET form
<form action="index.php" method="GET">
<input type="text" name="var" value="foo">
<input type="submit" name="submit" value="Submit">
</form>
*/
// The above form would convert the form values into a URL
// like: "index.php?var=foo&submit=Submit
$var = $_GET['var']; // $var equals "foo"


#6 freenity

freenity
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 10 August 2006 - 07:33 PM

eys it was because of this thanks a lot




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users