I want to use the php session to pass the username to the second page
but it shows error.
Notice: Undefined index: nam in D:\software 2\wamp\wamp\www\session\s2.php on line 5
my first page(s1.php) is like this
<html>
<head>
<?php
session_start();
?>
</head>
<body>
<p>hello</p>
<form method="get" action=" http://localhost/session/s2.php">
<input type="text" name="nam"><br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>
my second page (s2.php) is bellow
<html>
<head>
<?php
session_start();
echo $_SESSION['nam'];
?>
</head>
here is the second page
<body>
</body>
</html>
thanks
Variables are not inserted into the session automatically. You need to insert them somehow. If you want to get the variables from the get paramaters posted by the form $_GET is what you are looking for.
eg:
$_SESSION['name'] = $_GET['name'];
Have a look through dealing with forms
You need to put session start at the top
<?php
session_start();
?>
<html>
<head>
You're most likely not seeing the error that is generated.
Also, after the data is submitted, you won't have it in the SESSION yet, it will be in $_GET['name']
I would recommend that you set the error_level to E_ALL while working on your local machine
In PHP, variables from a form are passed via GET or POST. In your case, you used
<form method="get" action=" http://localhost/session/s2.php">
So the type is GET (method="get"). To read GET variables, use _GET, not _SESSION. If you want POST variables, use _POST.
In your case, use _GET['nam'] instead of _SESSION['nam']
you should either :
add this line before echo $_SESSION['nam'];
$_GET['nam']=$_SESSION['nam'];
or
echo $_GET['nam'];
Good Luck
Related
I am new to PHP. I have written a simple code to collect data from a form using PHP. I am using the POST Method to collect the form data but it goes to the else statement. Can someone please help me out.
Thanks in advance!!
My Code-
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post">
<input type="text" id="name" name="name">
<input type="submit" name="submit" id="submit">
</form>
<?php
if(isset($_POST)){
$x = $_POST['name'];
echo $x;
}else {
echo "failed";
}
?>
</body>
</html>
Don't you need an attribute called "action" to perform something like that? Or maybe it isn't needed if it is in the same file
I think the culprit here is the isset function call. That function returns false even if a variable has been set but equals null, which I believe is what's happening here. Are you supplying any input before submitting?
I recommend using this to check if the form was submitted instead.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
...
}
You can then process the inputs in that block. You might also try checking for !empty instead of isset if you want to keep exploring your script as it is now.
Former answer was incorrect, time for an update.
Running your original code locally on OS X 10.14 using php 7.1 works fine.
Do you have a local server setup? How are you accessing the page that isn't behaving as expected? What happens when you place <?php phpinfo(); ?> in the same file?
Hi I'm learning php of a tutorial and these are my two files. I browse to my apache2 server via http://myservers-ip/form2.php
fill out the forms and hit the submit button, it calls my result.php page, but all it displays is "Hi ." where it should be like "Hi (userentry)."
Please help :-(
form2.php:
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>Enter your name</h1>
<form method="post" action="result.php">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
and my result.php
Hi <?php print $username; ?>.
Using apache2 and mysql running on my box.
I'm not sure if the source code is correct or if there might be a misconfiguration? if so which config files would you need?
Thanks
Data sent via a form with POST action will be in the superglobal $_POST array. You would want to sanitize it before trying to use it for anything, but just starting with $_POST['username'] will get you closer to your end goal.
Edit: Whatever tutorial you are using, abandon it. It's clearly waaaaay outdated.
You're sending data from form via post so you need to get them in your result.php file from POST superglobal like so:
Hi <?php print $_POST['username']; ?>.
The data is being sent to results.php via POST method.
All post params are stored in $_POST param. So to get user name you need to get it from $_POST, in results.php. E.g:
<?php
// file: results.php
if (isset($_POST['username']){
echo "Hi, {$_POST['username']}.";
} else {
echo "No user name."
}
I'm a newbie learning and trying to understand how html forms and php processing works.
Came across this pair of examples:
HTML FORM:
<html>
<body>
<form action="hello-web.php" method="GET">
<label>Name:</label>
<input type="text" name="yourName" size="24">
<input type="submit">
</form>
</body>
</html>
PHP PROCESSOR:
<?php
$fname = $_GET["yourName"];
echo "Hello $fname!";
?>
OUTPUT SHOULD BE:
Hello Entered/Example Name!
QUESTION:
When I try to change the variable "yourName" (on BOTH HTML and PHP files) to, for example "typeName" , the entered name on the form does not show up.
In other words, the output becomes just: Hello !
Is "yourName" a standard php or html variable? Can it not be changed to what ever you want it to be?
Better yet, how exactly does the form process data?
Here is my altered code that won't output the entered name (I posted here as an answer because all the codes shows up as a continuous line, like a paragraph, when I paste as a comment to your answer:
HTML FORM(altered--typeName):
<html>
<body>
<form action="hello-web.php" method="GET">
<label>Name:</label>
<input type="text" name="typeName" size="24">
<input type="submit">
</form>
</body>
</html>
PHP PRCESSOR (altered--typeName):
<html>
<body>
<?php
$fname = $_GET["typeName"];
echo "Hello $fname!";
?>
</body>
</html>
You can see what data is available to you from the submitted form by outputting the entire array. Since your form method is GET, the following will show you all that was submitted:
var_dump( $_GET );
From this, you can see what the variable names should be in your PHP script.
Array
(
[YourName] => Jonathan
)
Anytime you come across a disconnect between what is being submitted, and what you're expecting, check $_GET (or if your method is POST, you would check $_POST).
For instance, if I were trying the following:
echo $_GET["yourName"]; // nothing output to the screen
I could refer to the array contents printed above and see that the correct key is "YourName":
echo $_GET["YourName"]; // Jonathan
$_GET["yourName"]; Contains a value based off of the input of the form field. It's a php superglobal http://us3.php.net/manual/en/reserved.variables.get.php
It sounds like you're changing the html form, but your not entering a value through the form. Therefore, $_GET["yourName"]; is empty
I am trying to create a very simple session between 3 php pages as: index.php ,validate.php and target.php
index.php
<?php
session_start();
$_SESSION['uid'] = 'test';
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Start A Session</title>
</head>
<body>
<h1>Welcome to Heaven</h1>
<form method="POST" action="validate.php">
Your Name: <input type="text" name="name" value="" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
and validate.php as:
<?php
session_start();
$err="Not Allowed";
if(($_POST['name']) == $_SESSION['uid']){
header ("Location: heaven.php");}
else
{echo $err; }
?>
and finally target.php as
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Start Email with PHP</title>
</head>
<body>
<h1>Welcome to Targer <?php echo $_SESSION['uid'] ?></h1>
<img src="session.jpg">
</body>
</html>
Now my questions are:
How come user still can get access to target page while I have already set a session between pages(according to my understanding of sessions the target page must NOT be accessible unless the correct seesion value has been submitted but I can get access to page by clicking on page link in wamp index list! with echoing the $err; ! )
I tried to validate the $_SESSION value by using the isset() but it did'nt go through! can you please let me know how I can modify the validate.php using the isset() instead of if(($_POST['name']) == $_SESSION['uid']) comarison?
Can you please let me know how I can merge two (index.php and validate.php) in one page? I mean how I can validate the session inside the index.php and reduce the files two index and target? In this case I need to handle the wrong logins inside the index page.
Finally, can you please let me know how I can assign the value to $_SESSION from user input? I mean instead of having a hard coded part like $_SESSION['uid'] = 'test'; let the session value start with user input! I know this looks meaningless here but I would like to get idea on creating captcha in same index page
Thanks for your time in advance
This should work properly and just with 2 files. You could even compress it to 1 file, if you really want so (it's much harder to understand), ask for it and I can make it.
index.php:
<?php
session_start();
$_SESSION['uid'] = 'test';
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Start A Session</title>
</head>
<body>
<h1>Welcome to Heaven</h1>
<form method="POST" action="heaven.php">
Your Name: <input type="text" name="name" value="" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
And the other file, heaven.php
<?php
session_start();
if (!empty($_SESSION['uid'])&&$_POST['name']==$_SESSION['uid'])
{
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Start Email with PHP</title>
</head>
<body>
<h1>Welcome to Targer <?php echo $_SESSION['uid'] ?></h1>
<img src="session.jpg">
</body>
</html>
<?php
}
else
{
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Access denied</title>
</head>
<body>
Sorry, you have no permission to access this page.
</body>
</html>
<?php
}
?>
'Validation' is just to display the desired message if the strings match or to display a 'you cannot see this page' if they don't match.
The !empty() needs to be set. Else, imagine what would happen if someone visited the 'heaven.php' page without going first to index.php. You'd compare 2 empty strings! So it would be validated. The alternative is to put $_SESSION['uid'] = 'test'; at the beginning of heaven.php also.
I didn't really answer your questions in order (and in the 2nd one I cheated as I put 2nd and 3rd file together instead of 1st and 2nd), but my code should cover all your problems. For the last part, it's simply $_SESSION['uid']=$_POST['name']; , but it'd be a no sense to do it while validating user. The validation must come from somewhere, generally from a mysql database.
Please start to accept answers to your questions if valid (below the up/down button).
1) What do you mean by assigning correct session value? "heaven.php" is a file which is accessible by anyone if you don't check for access rights in that file.
2) Not sure what you mean. If you need to check if two variables exist and match, you need something like if ( (isset($_POST['name']) && isset($_SESSION['uid'])) && (($_POST['name']) == $_SESSION['uid']) )
3) If there's no need to reduce files to minumun, it's a good idea to keep validating, form processing etc. function in their respective files. If you want to valide session and form values within "index.php" you can do so by first checking if the values exist and then do to them whatever you like. (Like in the 2 above.)
4) You can assign user inputted values for variables by using forms. It's what you are doing with your form currently. In you process file/function you do something like:
if(isset($_POST['submit']){
$_SESSION['uid'] == $_POST['name'];
}
The example above is very simplified.
Finally. You aren't really validating anything, you just check if two values match. Validating means that you make sure value meets the standard(s) specified, for example user submitted value is an integer when it should be an integer (when inserted into database or used some other manner). I think you should see some tutorials about form processing and user input validation before creating anything that's intended in production environment.
I jut recently started learning PHP from a book called PHP/MySQL Programming for the Absolute Beginner by Andy Harris.
In one of his samples, he writes that this code should function (I've cut it short a bit):
<html>
<head>
<title>Font Choices</title>
</head>
<body>
<center>
<h1>Font Choices</h1>
<h3>Demonstrates how to read HTML form elements</h3>
<form method = "post"
action = "borderMaker.php">
<h3>Text to modify</h3>
<textarea name = "basicText"
rows = "10"
cols = "40">
blah blah
</textarea>
<input type = "submit"
value = "show me">
</form>
</center>
</body>
</html>
Then in Bordermaker.php something like:
<html>
<head>
<title>Your Output</title>
</head>
<body>
<h1>Your Output</h1>
<center>
<?
print $basicText;
?>
</center>
</body>
</html>
But when i try to run it it says I can't find the $basicText variable.
I'm currently learning PHP with XAMPP running on my computer.
Thanks for any help.
use $_POST['basicText'] instead of $basicText
You are using the "post" method on your form so all your input values will be contained in an array called $_POST.
If you were using "get" as your method, all the form information would be passed as a query string on the end of your url, and you would access them with an array called $_GET the same way.
This example relies on the usage of register_globals to function. What this means is that every variable submitted via form becomes a global variable, which means random-joe can inject unknown variables into your code. This has been deprecated as of 5.3 and will be removed in later versions.
I highly recommend finding a book based on later versions of PHP to learn from, preferably something tailored to PHP 5 (at minimum).