如果登录而不是if(!isset($ submit))我该如何使用

时间:2015-08-28 16:12:00

标签: php

我使用名字,姓氏和密码登录特定页面。这是登录页面代码:

<form action="?page=welcome" method="post" enctype="multipart/form-data" name="form1" target="_self">
   First name:
  <input name="firstname" type="text" id="title" size="26" maxlength="40" value="<? echo $firstname ?>"> 
  Last name:
  <input name="lastname" type="text" id="title" size="26" maxlength="40" value="<? echo $lastname ?>"> 
  Password:
  <input name="pwd" type="password" id="pwd" value="<? echo $pwd ?>"> 
  <input name="submit" type="submit" value="Submit" class="button"> 
</form>

以下是欢迎页面的代码:

<? include("dbconnection.php");?>
<?php
$phpver = phpversion();
$phpver = explode(".", $phpver);
$phpver = "$phpver[0]$phpver[1]";
if ($phpver != 41) {
$PHP_SELF = $_SERVER['PHP_SELF'];
}
if (!ini_get("register_globals")) {
import_request_variables('GPC');
} 

if (!isset($submit)) {
show_login_form($firstname,$lastname,$password);
exit;
}

if (!($firstname&&$lastname&&$password)) {  

    echo "<br><br><center><span class='alertext' align=left>Please fill in all required fields to proceed.<br>";
echo '<a href="javascript: history.go(-1)">Go back</a></span>';
show_login_form($firstname,$lastname,$password);
exit;

} else {
$firstname=trim(addslashes("$firstname"));
$lastname=trim(addslashes("$lastname"));
$password=trim("$password");    

$log="SELECT * FROM application WHERE UCASE(firstname)=UCASE('$firstname') AND lastname='$lastname' AND password='$password'";
$query=mysql_query($log);
$info=mysql_fetch_array($query);

if (!$query or $info[0]==NULL) {

    echo "<br><br><center><span class='alertext' align=left>The login information is incorrect. Please try again.<br>";
echo '<a href="javascript: history.go(-1)">Go back</a></span>';
    show_login_form($firstname,$lastname,$password);
    exit;

} else {        

    $cat="SELECT * FROM TABLE WHERE id='$info[0]'";
    $query2=mysql_query($cat) or die("Error".mysql_error);
    $info2=mysql_fetch_row($query2);

    $send="SELECT * FROM TABLE WHERE id='$info[0]'";
    $info3=mysql_fetch_row($query3);            
    $id=$info[0];

    $sql_name = "SELECT * FROM TABLE WHERE id='$info[0]'";
    $res_name = mysql_query($sql_name);
    $first_row = 1;
    while ($pinfo=mysql_fetch_array($res_name)) {           
        $explode = explode("-",$pinfo[name]);
    } ?>
<? }
 } 
?>`

这很好用。但我想有第三页。当我在第三页上使用相同的代码(有一个表单和提交按钮)时,由于用于登录的isset提交代码,它会自动提交页面。我尝试使用if登录代码而不是isset submit但没有成功。我将不胜感激任何意见或建议。谢谢。

现在我正在尝试在第2页上添加page3编辑表单:这是我转到第2页的编辑表单代码: <form name="form2" method="post" onSubmit="return checkForm(this);" enctype="multipart/form-data" action="?page=page3"> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="16" height="38" bgcolor="#eaeaea"></td> <td width="1280" valign="middle" bgcolor="#eaeaea"><span class="heading">Editing Application </span><br> </td> <td width="16" bgcolor="#eaeaea"></td> </tr> <tr> <td background="../img/box/left_line.jpg"></td> <td> <table width="100%" border="0.1" align="left" class="formtext"> <tr> <td class="formtext"><br> First name:<span class="style4">*</span><br> <input name="firstname" type="text" class="style5" id="firstname" value="<?php echo "$info[firstname]"; ?>" size="50" maxlength="100" /> <br> <br> Last name:<span class="style4">*</span><br> <input name="lastname" type="text" class="style5" id="lastname" value="<?php echo "$info[lastname]"; ?>" size="50" maxlength="100" /> <br> <br> Are you?<span class="style4">*</span> <SELECT name="applicant_type" class="style5" id="applicant_type"> <OPTION VALUE="<? echo "$info[applicant_type]"; ?>"><? echo "$info[applicant_type]"; ?></OPTION> <OPTION value="Student">Student</OPTION> <OPTION value="Professor">Professor</OPTION> <OPTION value="Professional">Professional</OPTION> </SELECT> <br> <br> Email Address:<span class="style4">*</span><br> <input name="email" type="text" class="style5" id="email" value="<?php echo "$info[email]"; ?>" size="50" maxlength="100" /> <br> <br> Nation:<br> <input name="nation" type="text" class="style5" id="nation" value="<?php echo "$info[nation]"; ?>" size="50" maxlength="100" /> <br> <br> University (or corporate) name:<span class="style4">*</span><br> <input name="college" type="text" class="style5" id="college" value="<?php echo "$info[college]"; ?>" size="70" maxlength="100" /> <br> <br> Academic (or corporate) department:<br> <input name="department" type="text" class="style5" id="department" value="<?php echo "$info[department]"; ?>" size="70" maxlength="150" /> <br> <span class="formnotes">(History, Mathematics, Psychology, Chemical Engineering, etc.)</span> <br> <br> Degree Major:<br> <input name="major" type="text" class="style5" id="major" value="<?php echo "$info[major]"; ?>" size="50" maxlength="150" /> <br> <span class="formnotes">(Anthropology, Biology, Chemistry and etc.) </span> <br> <br> Degree pursued (or degree complete for non-students):<br> <input name="degree" type="text" class="style5" id="degree3" value="<?php echo "$info[degree]"; ?>" size="55" maxlength="150" /> <br> <span class="formnotes">(Masters, PhD, etc.) </span> <br> </td> </tr> <tr> <td> <input name="submit2" type="submit" id="submit" value="Submit" class="button"> </td> </tr> </table></td> <td valign="baseline" background="../img/box/right_line.jpg"></td> </tr> <tr> <td><img src="../img/box/left_corner.jpg" width="16" height="14"></td> <td background="../img/box/bottom_line.jpg"></td> <td><img src="../img/box/right_corner.jpg" width="16" height="14"></td> </tr> </table> </form>

当您单击提交按钮时,我将转到第3页以设置更新表数据。以下是page3的代码:

`<? session_start();
$phpver = phpversion();
$phpver = explode(".", $phpver);
$phpver = "$phpver[0]$phpver[1]";
if ($phpver != 41) {
$PHP_SELF = $_SERVER['PHP_SELF'];
}
if (!ini_get("register_globals")) {
import_request_variables('GPC');
}  

?>
<?php

include ("dbconnection.php");

    $query1 = "UPDATE TABLE SET ran = '$ran', firstname = '$firstname', lastname = '$lastname', applicant_type = '$applicant_type', email = '$email', 
    nation = '$nation', college = '$college', department = '$department', major = '$major', degree = '$degree' WHERE id='$id'";

    $result1 = mysql_query($query1) or die ('Could not run query: '.mysql_error());

    if ($result1) { echo "<br><br><center><span class='alertext'>Successfully modified!<br>(a copy of your information has been sent to you for your records)</span>"; ?>
 <center> 
<br> 
<span class="formtext">:: <a href="javascript: history.go(-1)">Go back</a> ::</span><br> 
<br> 
<span class="formtext">:: <a href='http://website.com'>Log out</a> ::</span>  <br> 
<?

}else { echo "<br><br><center><span class='alertext'>Error while modifying! </span>"; }

// sending confirmation messages

        if ($email<>NULL) 
        {
            $message = "Dear $firstname $lastname, \n\n";
            $message .= "REST of the Message.\n\n";

            $from = "From: Name <email@address.com> \n";
            $reply = "Reply-To: email@address.com ";
            $header .= $from . $reply;
            $sentmail = mail($email, "Subject", $message, $header);

        }
        if ($sentmail) 
        {

            //Copy of the message sent to Me

            $message = "Subject message sent by $firstname $lastname on ".date("Y-m-d")."\n\n";
            $message .= "REST of the Message\n";

            $message .= "Sincerely,\n";
            $message .= "Me \n\n";

            $from = "From: ".$name." <".$email."> \n";
            $reply = "Reply-To: ".$name." <".$email.">";
            $header = $from . $reply;
            $email2 = "email@address.com";
            mail($email2, "Subject", $message, $header);
}
?> 
</form>`

3 个答案:

答案 0 :(得分:2)

花了一两分钟,这是我从您的问题中理解的内容,您希望在继续使用该新页面之前检查您是否来自特定的页面。有几种方法可以实现这一目标:

  
      
  1. $ _ SESSION
  2.   

在您的旧/新两个页面的开头写session_start();,然后在您的上一页上存储会话中的值,如:

$_SESSION['check'] = true;

在下一页上应用支票:

if(isset($_SESSION['check']){
// way to go

同样,您也可以使用$_COOKIE,具体取决于您的要求/条件。

  
      
  1. $ _ GET
  2.   

如果你从网址传来。传递上一页的查询字符串,如:

$check = 1;
< a href = "newpage.php?check=<?php echo $check; ?>">Next Page</a>

在下一页上,将其取为:

if(isset($_GET['check']){
// $chk = $_GET['check']; and blah
  
      
  1. $ _ POST
  2.   

就像你已经在某种程度上使用过一样:

<input name="submit" type="submit" value="Submit" class="button">

以上是从您的代码中复制的行,现在:

if(isset($_POST['submit']) && $_POST['login'] == submit){
// making sure that you're redirected from login page blahh 

答案 1 :(得分:0)

您可以通过查询字符串传递另一个变量并检查其存在。或者任何其他$_POST$_SESSION类型变量可以帮助您检查if条件来检查您来自哪里。我希望我能正确理解你的问题。

答案 2 :(得分:0)

您可以将提交按钮的名称设置为&#34;登录&#34;然后在代码测试中:

if(isset($_POST['login']) && $_POST['login'] == 'submit') {
    //process the form data
}