我有这个联系表格。我希望在填写表单并单击提交按钮后,我的脚本会检查用户是否已登录。如果用户未登录,则会将其重定向到登录页面进行登录,之后,他会看到&#39 ;感谢填写表格'。我不知道如何执行此操作,我的脚本在登录后将用户重定向回空联系表单,然后用户将再次填写字段。我该如何解决这个问题?
contact.php
<?php
session_start();
$location= urlencode($_SERVER['REQUEST_URI']);
if (isset($_SESSION['id']) == false){
echo "You have to be <a href='login.php?location=$location'>logged in</a>";
}else{
//Insert into database
//thanks for filling the form
}
?>
//Contact form fields
的login.php
<?php
if (login details is true) {
$redirect = NULL;
$redirect = $_GET['location'];
header("Location:". $redirect);
}
?>
答案 0 :(得分:1)
<?php //Start the Session
session_start();
require('connect.php');
//If the form is submitted or not. and If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])){
//Assigning posted values to variables.
$username = $_POST['username'];
$password = $_POST['password'];
//Checking the values are existing in the database or not
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
//If the posted values are equal to the database values, then session will be created for the user.
if ($count == 1){
$_SESSION['username'] = $username;
}else{
//If the login credentials doesn't match, he will be rediret with an error message.
header("Location:yourpage.php?msg=Invalid Login Credentials");
exit;
}
}
//if the user is logged in Greets the user with message
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo "Hai " . $username . "
";
echo "This is the Members Area
";
echo "<a href='logout.php'>Logout</a>";
}else{
header("Location:register.php");
exit;
}
?>
答案 1 :(得分:0)
登录后将用户凭据转换为会话,例如他的联系人ID,联系人姓名,以便在整个项目中使用此进行此类检查.....
在联系表格中,如果用户登录到您的网站,您将获得会话详细信息,例如,如果他以$_SESSION['CONTACT_ID'];
身份登录,则将他的联系人ID转换为会话
然后,您可以检查顶部的此会话ID联系页面
<?
$redirect = "login.php";
if($_SESSION['CONTACT_ID']=='')
{
header("Location:". $redirect);
}
?>
如果用户未登录,则他将无法使用联系页面,并将被重定向到登录页面
答案 2 :(得分:0)
首先,您必须保存用户放在每个字段中的内容,您可以将$ _POST传递给$ _SESSION;
\\
当用户登录时,您可以在字段(html表单)中检索这些数据$ _SESSION:
session_start();
$_SESSION['example'] = $_POST['dataFromForm'];
...