freenity Posted August 10, 2006 Share Posted August 10, 2006 HiToday Ive instaled apache 2.2 with php 5 on my machine and have some strange problem. This is the code:<?phpif (isset($ok)) echo "hi";else echo "bye";?>and it always prints "bye" even if I enter like this: localhost/file.php?ok=asdit seems that it doesnt detect that variable was set. Might some problem with configuration. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/17164-strange-problem-with-variables/ Share on other sites More sharing options...
Ninjakreborn Posted August 10, 2006 Share Posted August 10, 2006 [code]<?phpif (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 doif (isset($_GET['ok'])) {instead of what you have. Quote Link to comment https://forums.phpfreaks.com/topic/17164-strange-problem-with-variables/#findComment-72670 Share on other sites More sharing options...
freenity Posted August 10, 2006 Author Share Posted August 10, 2006 [quote author=businessman332211 link=topic=103758.msg413416#msg413416 date=1155237404][code]<?phpif (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 doif (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. Quote Link to comment https://forums.phpfreaks.com/topic/17164-strange-problem-with-variables/#findComment-72674 Share on other sites More sharing options...
obsidian Posted August 10, 2006 Share Posted August 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/17164-strange-problem-with-variables/#findComment-72677 Share on other sites More sharing options...
HeyRay2 Posted August 10, 2006 Share Posted August 10, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/17164-strange-problem-with-variables/#findComment-72680 Share on other sites More sharing options...
freenity Posted August 10, 2006 Author Share Posted August 10, 2006 eys it was because of this thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/17164-strange-problem-with-variables/#findComment-72684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.