Jump to content

session help


archer39

Recommended Posts

Hello,

 

I am trying to create an HTML/PHP form to collect user data, then use PHP to create a server side session to store the information. I i need this to navigate to a second page that will display the session information.

 

Here is the code i have so far:

 

num2.html

 

<?

session_start();

 

$username = $_GET['username'];

$password = $_GET['password'];

 

$_SESSION['username']= $username;

$_SESSION['password']= $password;

 

?>

 

<html>

 

 

<head>

  <title>Num 3</title>

</head>

<body>

 

<form action="form2.php" method="GET">

<p>Enter User Name: <input name="username" type="text" id="username" /></p>

<p>Enter Password: <input name="password" type="password" id="password" /></p>

<p><input type="submit" /></p>

</form>

 

</body>

 

</html>

 

 

form2.php

 

<?php

session_start();

 

 

echo "Username: ".$_SESSION['username'];

 

echo "Password: ".$_SESSION['password'];

?>

 

 

i get this:

 

Notice: Undefined index: username in /.../dce.psu.edu/fs/users/z/t/ztm5007/www/ist240/form2.php on line 14

Username:

Notice: Undefined index: password in /.../dce.psu.edu/fs/users/z/t/ztm5007/www/ist240/form2.php on line 16

Password:

 

when i try to access the page.

 

any help would be greatly appreciated. on a side not this i my first time using php so i am very unfamiliar with it.

 

Link to comment
https://forums.phpfreaks.com/topic/103815-session-help/
Share on other sites

Your username/password variables are being sent to form2 so

 

$username = $_GET['username'];
$password = $_GET['password'];

$_SESSION['username']= $username;
$_SESSION['password']= $password;

would need to be on form2.php.

 

On an important side note:

 

Its better to use POST for sending this data rather than GET.

Link to comment
https://forums.phpfreaks.com/topic/103815-session-help/#findComment-531489
Share on other sites

<html>

<head>
  <title>Num 3</title>
</head>
<body>

<form action="form2.php" method="POST">
<p>Enter User Name: <input name="username" type="text" id="username" /></p>
<p>Enter Password: <input name="password" type="password" id="password" /></p>
<p><input type="submit" name='submit' value='Submit' /></p>
</form>
</body>
</html>

<?php
//form2.php
session_start();

if(isset($_POST['submit'])){
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
}

echo "Username: ".$_SESSION['username'];
echo "Password: ".$_SESSION['password'];
?>

I didn't test this but it should be close to if not correct.

Link to comment
https://forums.phpfreaks.com/topic/103815-session-help/#findComment-531494
Share on other sites

<html>

<head>
  <title>Num 3</title>
</head>
<body>

<form action="form2.php" method="POST">
<p>Enter User Name: <input name="username" type="text" id="username" /></p>
<p>Enter Password: <input name="password" type="password" id="password" /></p>
<p><input type="submit" name='submit' value='Submit' /></p>
</form>
</body>
</html>

<?php
//form2.php
session_start();

if(isset($_POST['submit'])){
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
}

echo "Username: ".$_SESSION['username'];
echo "Password: ".$_SESSION['password'];
?>

I didn't test this but it should be close to if not correct.

 

 

i get this when running it:

 

Notice: Undefined index: username in /.../dce.psu.edu/fs/users/z/t/ztm5007/www/ist240/form2.php on line 10

Username:

Notice: Undefined index: password in /.../dce.psu.edu/fs/users/z/t/ztm5007/www/ist240/form2.php on line 11

Password:

Link to comment
https://forums.phpfreaks.com/topic/103815-session-help/#findComment-531498
Share on other sites

The notice is not an error. You have error reporting set to a level that is too high.

 

User : error_reporting(E_ALL ^ E_NOTICE); at the top of your page (for now).  Change the php.ini error_reporting settings.

 

num2.html

<html>


<head>
  <title>Num 3</title>
</head>
<body>

<form action="form2.php" method="POST">
<p>Enter User Name: <input name="username" type="text" id="username" /></p>
<p>Enter Password: <input name="password" type="password" id="password" /></p>
<p><input type="submit" /></p>
</form>

</body>

</html>

 

form2.php

<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();

$username = $_POST['username'];
$password = $_POST['password'];

$_SESSION['username']= $username;
$_SESSION['password']= $password;


echo "Username: ".$_SESSION['username'];

echo "Password: ".$_SESSION['password'];
?>

Link to comment
https://forums.phpfreaks.com/topic/103815-session-help/#findComment-531514
Share on other sites

I have never tried setting form action to a separate page so I can't recognize the way things need to be. Try this

<?php
session_start();
if(isset($_POST['submit'])){
   $_SESSION['username'] = $_POST['username'];
   $_SESSION['password'] = $_POST['password'];

   $linkContinue = "<a href='form2.php'>Click here to continue</a>";

}
?>
<html>

<head>
  <title>Num 3</title>
</head>
<body>
<?php echo $linkContinue ?>
<form action="num3.php" method="POST">
<p>Enter User Name: <input name="username" type="text" id="username" /></p>
<p>Enter Password: <input name="password" type="password" id="password" /></p>
<p><input type="submit" name='submit' value='Submit' /></p>
</form>
</body>
</html>

<?php
//form2.php
session_start();


echo "Username: ".$_SESSION['username'];
echo "Password: ".$_SESSION['password'];
?>

Link to comment
https://forums.phpfreaks.com/topic/103815-session-help/#findComment-531681
Share on other sites

Archived

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

×
×
  • Create New...

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.