これは簡単な PHP mySQL の質問です (PHP と mySQL 101 のようなものだと思いますが、答えを見つけるのに苦労しています)...
作成した登録フォームがあり、フォームに入力して送信すると、register_user.php ページに投稿されます。そのページのコードは次のとおりです。
<?php
$con = mysql_connect("my_host","my_username","my_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
// username and password sent from form
//NEVER Remove the mysql_real_escape_string. Else there could be an Sql-Injection!
$firstname=mysql_real_escape_string($_POST['firstname']);
$lastname=mysql_real_escape_string($_POST['lastname']);
$email=mysql_real_escape_string($_POST['email']);
$password=mysql_real_escape_string($_POST['password']);
$sql="INSERT INTO my_table (firstName, lastName, email, password)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[email]', '$_POST[password]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
session_start();
// Register $myusername, $mypassword and redirect to file "profile.php"
$_SESSION['firstname']='$_POST[firstname]';
$_SESSION['lastname']='$_POST[lastname]';
$_SESSION['email']='$_POST[email]';
$_SESSION['password']='$_POST[password]';
header("location:profile.php");
?>
ご覧のとおり、 profile.php というページにリダイレクトされます。profile.php で、次のように言ってセッション変数をエコーアウトしようとしています:
<?php echo "Hello". $_SESSION['firstname']; ?>
register_user.php から profile.php へのリダイレクト時に変数の値が失われると想定しています (少なくともこれは、これまでの調査から収集したものです)。profile.php を使用session_start();
して変数を再度宣言する必要があると考えていますが、変数を適切な値に設定する方法がわかりません。register_user.php では、前のページのフォームからの投稿値を使用できましたが、profile.php にリダイレクトされるまでにはそれができなくなりました。
profile.php でこれらの値を取得する方法を誰か説明してもらえますか? (繰り返しますが、これは非常に基本的な質問だと感じていますが、私はまだ学んでいるので、優しくしてください :) )