Jump to content

Archived

This topic is now archived and is closed to further replies.

freenity

strange problem with variables

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[code]<?php


if (isset($ok)) {
  echo "hi";
}else {
  echo "bye";
}
?>[/code]
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.

Share this post


Link to post
Share on other sites
[quote author=businessman332211 link=topic=103758.msg413416#msg413416 date=1155237404]
[code]<?php


if (isset($ok)) {
  echo "hi";
}else {
  echo "bye";
}
?>[/code]
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.

[/quote]

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.

Share this post


Link to post
Share on other sites
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:
[code]
<?php
$ok = isset($_GET['ok']) ? $_GET['ok'] : '';

if (!empty($ok)) echo "hi!";
else echo "bye!";
?>
[/code]

so, on the server that it worked on, you must have had register_globals on.

Share this post


Link to post
Share on other sites
PHP 5 has a setting called [b]Register Globals[/b] 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 [b]businessman332211[/b] mentioned.

Values passed through a URL will be assigned in the [b]$_GET[/b] array:

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

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

[code=php:0]
/* 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"
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.