473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PHP variables are always empty

I'm trying to write a PHP script which allows to enter an username and a
password and to click two buttons.
If I start my PHP scripts 'mytest.php' I see the two input fields and the
two buttons. However if I enter something to the input fields and press a
button the echo commands show nothing.

======

PHP script 'mytest.php'
-----------------------

<?php
session_start();

if (!isset($_SESSION['auth_ok'])) {
$_SESSION['auth_ok'] = false;
}

echo "$user <br>";
echo "$pass <br>";
echo "$button1 <br>";
echo "$button2 <br>";
echo "$auth_ok <br>";

if (isset($button1)){
echo "Button 1 has been pressed<br>";
$auth_ok = true;
}

if (isset($button2)){
echo "Button 2 has been pressed<br>";
$auth_ok = true;
}
?>

<html>
<body>
<form name="loginform" method="post" action="mytest.php">
Username: <input type="text" name="user" size="30" maxlength="30"><br>
Passwort: <input type="password" name="pass" size="30"
maxlength="30"><br>
<input type="submit" name="button1" value="Button 1">
<input type="submit" name="button2" value="Button 2">
</form>
</body>
</html>

======

The PHP error logs shows the following errors:
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: user in
D:\WWW\php\mytest.php on line 8
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: pass in
D:\WWW\php\mytest.php on line 9
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: button1 in
D:\WWW\php\mytest.php on line 10
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: button2 in
D:\WWW\php\mytest.php on line 11
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: auth_ok in
D:\WWW\php\mytest.php on line 12

I'm very confused because my colleague told me that the same script on his
machine is running without any problems.
Has someone any idea what's wrong with my script or my server?

Stefan
Jul 17 '05 #1
5 2589
Stefan Mueller wrote:
PHP script 'mytest.php'
-----------------------

<?php
session_start();

if (!isset($_SESSION['auth_ok'])) {
$_SESSION['auth_ok'] = false;
}

echo "$user <br>";
echo "$pass <br>";
echo "$button1 <br>";
echo "$button2 <br>";
echo "$auth_ok <br>";
Your collegue is using a machine configured with register_globals on
(not reccomended) and hasn't been the default setting for quite some
time.
use this instead:
echo $_POST['user']."<br>";
echo $_POST['pass']."<br>";
echo $_POST['button1']."<br>";
echo $_POST['button2']."<br>";
echo $_POST['auth_od']."<br>";
// etc...

I could be wrong, but there's a good chance both buttons will be set..
use if ( !empty($_POST['button1']) ) instead

?>
I'm very confused because my colleague told me that the same script on his machine is running without any problems.
Has someone any idea what's wrong with my script or my server?

Stefan


Jul 17 '05 #2
Stefan Mueller wrote:
I'm trying to write a PHP script which allows to enter an username and a
password and to click two buttons.
If I start my PHP scripts 'mytest.php' I see the two input fields and the
two buttons. However if I enter something to the input fields and press a
button the echo commands show nothing.

======

PHP script 'mytest.php'
-----------------------

<?php
session_start();

if (!isset($_SESSION['auth_ok'])) {
$_SESSION['auth_ok'] = false;
}

echo "$user <br>";
echo "$pass <br>";
echo "$button1 <br>";
echo "$button2 <br>";
echo "$auth_ok <br>";

if (isset($button1)){
echo "Button 1 has been pressed<br>";
$auth_ok = true;
}

if (isset($button2)){
echo "Button 2 has been pressed<br>";
$auth_ok = true;
}
?>

<html>
<body>
<form name="loginform" method="post" action="mytest.php">
Username: <input type="text" name="user" size="30" maxlength="30"><br>
Passwort: <input type="password" name="pass" size="30"
maxlength="30"><br>
<input type="submit" name="button1" value="Button 1">
<input type="submit" name="button2" value="Button 2">
</form>
</body>
</html>

======

The PHP error logs shows the following errors:
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: user in
D:\WWW\php\mytest.php on line 8
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: pass in
D:\WWW\php\mytest.php on line 9
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: button1 in
D:\WWW\php\mytest.php on line 10
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: button2 in
D:\WWW\php\mytest.php on line 11
[22-Apr-2005 19:02:17] PHP Notice: Undefined variable: auth_ok in
D:\WWW\php\mytest.php on line 12

I'm very confused because my colleague told me that the same script on his
machine is running without any problems.
Has someone any idea what's wrong with my script or my server?

Stefan


Do you have register_globals on or off in your ini file? If it is off
(as it should be) you will have to access your form variables like this:

echo $_POST['user'] . "<br>";
echo $_POST['$pass'] . "<br>";

etc. etc.
Jul 17 '05 #3
First of all thanks a lot for your answers. I appreciate that really very
much.

However, I still have some Questions:

1) Why is the variable auth_ok still always empty?

2) Why do I have the following error messages in the PHP error log depending
on the button I pressed before?
[23-Apr-2005 23:14:28] PHP Notice: Undefined index: button1 in
D:\WWW\php\mytest.php on line 10
[23-Apr-2005 23:14:29] PHP Notice: Undefined index: button2 in
D:\WWW\php\mytest.php on line 11

============

<?php
session_start();

if (!isset($_SESSION['auth_ok'])) {
$_SESSION['auth_ok'] = false;
}

echo $_POST['user'] . "<br>";
echo $_POST['pass'] . "<br>";
echo $_POST['button1'] . "<br>";
echo $_POST['button2'] . "<br>";
echo $_SESSION['auth_ok'] . "<br>";

if (isset($_POST['button1'])){
echo "Button 1 has been pressed<br>";
$auth_ok = true;
}

if (isset($_POST['button2'])){
echo "Button 2 has been pressed<br>";
$auth_ok = true;
}
?>

<html>
<body>
<form name="loginform" method="post" action="mytest.php">
Username: <input type="text" name="user" size="30" maxlength="30"><br>
Passwort: <input type="password" name="pass" size="30"
maxlength="30"><br>
<input type="submit" name="button1" value="Button 1">
<input type="submit" name="button2" value="Button 2">
</form>
</body>
</html>

============
Jul 17 '05 #4
"Stefan Mueller" <se**************@yahoo.com> wrote:
First of all thanks a lot for your answers. I appreciate that really very
much.

However, I still have some Questions:

1) Why is the variable auth_ok still always empty?
Because $auth_ok and $_SESSION['auth_ok'] are two entirely different
variables. You didn't finish fixing your code. Perhaps you want
$auth_ok = $_SESSION['auth_ok'];
after that first "if" statement.
2) Why do I have the following error messages in the PHP error log depending
on the button I pressed before?
[23-Apr-2005 23:14:28] PHP Notice: Undefined index: button1 in
D:\WWW\php\mytest.php on line 10
[23-Apr-2005 23:14:29] PHP Notice: Undefined index: button2 in
D:\WWW\php\mytest.php on line 11


Because "button1" will only be present in the POST data if the user pressed
button1. In that case, "button2" will not be defined at all, and the
reference to $_POST['button2'] is a mistake.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 17 '05 #5
Yes, you're right.
Now my first PHP script is running. Cool!

Thanks a lot for your help.
Stefan
"Tim Roberts" <ti**@probo.com> wrote in message
news:r3********************************@4ax.com...
"Stefan Mueller" <se**************@yahoo.com> wrote:
First of all thanks a lot for your answers. I appreciate that really very
much.

However, I still have some Questions:

1) Why is the variable auth_ok still always empty?


Because $auth_ok and $_SESSION['auth_ok'] are two entirely different
variables. You didn't finish fixing your code. Perhaps you want
$auth_ok = $_SESSION['auth_ok'];
after that first "if" statement.
2) Why do I have the following error messages in the PHP error log
depending
on the button I pressed before?
[23-Apr-2005 23:14:28] PHP Notice: Undefined index: button1 in
D:\WWW\php\mytest.php on line 10
[23-Apr-2005 23:14:29] PHP Notice: Undefined index: button2 in
D:\WWW\php\mytest.php on line 11


Because "button1" will only be present in the POST data if the user
pressed
button1. In that case, "button2" will not be defined at all, and the
reference to $_POST['button2'] is a mistake.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nicole | last post by:
Hi I'm trying to use a function to set a session variable. I have three files: The first file has: <?php session_start(); // This connects to the existing session ?> <html> <head>
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
2
by: Steve Remer | last post by:
OK, I think I understand session state pretty well. I've done additional research on Google over the last few days to fill in any holes. To begin with, I'm using StateServer and not InProc for...
0
by: Shapper | last post by:
Hello, I have an ASP.NET/VB web page where I created a DataReader. With the SQL Query String I use I always get 0 to 4 records. The records have the following fields (I also include the MSSQL...
42
by: Dooglo | last post by:
I'm new VB and programming all together, but I'm getting he hang of it. My question is; I'm writting a program to figure square feet and yards when the user inputs "Length in feet and inch (...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
4
by: museumoftechno | last post by:
Hello I'm new to PHP session variables, and I'm doing some work for a client whose website uses session variables in a form that contains image verification. There's a problem with the form - it...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.