Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with var when not set?? 1

Status
Not open for further replies.

Elliott3

Programmer
Joined
Sep 5, 2002
Messages
347
Location
CA
I am a nebie to php..

I have two simple pages that pass info from one to the next. If I upload the files to a server running php, the pages work fine.

However, I have installed php on my local machine and need to do my testing from here. When I try to run the script, it causes a problem when a variable is not set. Is there a way around this(other than isset() test)? register_globals is set to ON on the server but not my local machine...im guessing that that may be the problem. I understand though that there are security issues when it is set to on.

here are my pages..

'check.html'
<html>
<body>

<form method = post action = &quot;check.php&quot;>

Have you ever eaten haggis before?
<input name = &quot;Choice1&quot; type = &quot;checkbox&quot; value = &quot;Haggus&quot;>
<br>
Have you ever eaten snails before?
<input name = &quot;Choice2&quot; type = &quot;checkbox&quot; value = &quot;Snails&quot;>
<br>
Have you ever eaten locusts before?
<input name = &quot;Choice3&quot; type = &quot;checkbox&quot; value = &quot;Locusts&quot;>
<br>
<br>
<input type = submit>

</form>

</body>
</html>


'check.php'
<html>
<head></head>
<body>

<?php
echo $_POST['Choice1'] . &quot;<br>&quot;;

if (isset($_POST['Choice2']))
echo $_POST['Choice2'] . &quot;<br>&quot;;

echo $_POST['Choice3'] . &quot;<br>&quot;;
?>
</body>
</html>

The if statement does solve the problem but what other options do I have? Also, if register_globals is ON, does that mean that there is an immediate security problem?

CES
 
I don't see anything wrong with checking for a variables existence using isset() or array_key_exists(). I do it all the time in my own code.



On a side note, you can also pass those checkbox values in such a way that they appear as elements of a single array.

If you change your HTML to read:

Code:
<html><body><form method=post action=&quot;test_showpost.php&quot;>

Have you ever eaten haggis before?
<input name=&quot;Choice[haggus]&quot; type= &quot;checkbox&quot;>
<br>
Have you ever eaten snails before?
<input name=&quot;Choice[Snails]&quot; type=&quot;checkbox&quot;>
<br>
Have you ever eaten locusts before?
<input name=&quot;Choice[Locusts]&quot; type=&quot;checkbox&quot;><br>
<br>
<input type=submit>

</form></body></html>

Then check all three boxes, $_POST will look something like:

Code:
Array
(
    [Choice] => Array
        (
            [haggus] => on
            [Snails] => on
            [Locusts] => on
        )

)



Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks for the suggestion!
Does it make your code more secure using $_POST['var'] with register_globals set to OFF as compared to just $var when it is set to ON?

Also I seem to be having a problem with using header() to redirect to another page.

<%php
header('Location: exit;
%>

I have placed this at the very beginning of the page and nothing happens...when I view the source of the page after it has downloaded, it displays the code? I'm guessing that maybe I just have the wrong idea here...any idea?

CES
 
hahaha
Apparently I am a little tired! lol

Thank you for your help I will read up o the security.


CES
 
Also, is there a good reason for using '<?php' or '<?' ?

CES
 
I've always used &quot;<?php&quot;. I figure that way, there is no confusion between parsers if multple ones are on the machine. For example, ASP uses &quot;<?&quot; for code blocks.

Whether or not the short tag (&quot;<?&quot;) even works is determined by the php.ini setting short_open_tag.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Also, I am preparing to transcribe a site from ASP to PHP and I have always used '<%' to denote ASP...can you actually use '<?' for ASP as well?

CES
 
was jus checking...thanks..

CES
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top