dsz1966 2013-02-12 05:49
浏览 29
已采纳

PHP重定向和包含

I have a php file (login.php) that processes a mysql connection,

then redirects to a sort of member's page after successful login.

That works flawlessly.

HOWEVER, when I attempt to include("login.php") and use, say the $username variable, it shows me the html of the redirect, or in this case the output of header("location:members.html");

This may not be a flaw, possibly a function of php, if so, should i maybe segregate the login.php file to two files, one that checks and one that redirects if successful?

thanks in advance

FILE INFO: login.html

<html>

<head>
<title>Login</title>

</head>
<body>

<form action="login.php" method="post">

<table cellpadding=10>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></input></td>
</tr>

<tr>
<td>Password:</td>
<td><input type="password" name="password"></input></td>
</tr>

<tr>
<td colspan="2"><input type="submit"></input></td>
</tr>
</table>

</form>

</body>

</html>

login.php

<?php 
session_start();


$con = mysql_connect($host,$_POST['username'],$_POST['password']);

if(!$con)
{
    die("Could Not Connect!" . "
" . "Reason: " . mysql_error());
}
else
{
    $_SESSION['username'] = $_POST['username'];
    //header("Location:interact.html");
    echo "<script>window.location = 'http://localhost/interact.html'</script>";
}

?>

interact.html

<html>
<head>

<title>Nexus | Envoy</title>

</head>

<body>
<p><?php echo "WELCOME ". $_SESSION['username']; ?></p>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dongsheng6056 2013-02-12 07:02
    关注

    Firstly you should slice the html into header, content and footer. The content changes for each page and header and footer will remain same. Add session_start() and code to establish connection in the header file.

    Just to give you a rough idea...

    login.php

    <?php 
    session_start();
    
    
    $con = mysql_connect($host,$_POST['username'],$_POST['password']);
    
    if(!$con)
    {
        die("Could Not Connect!" . "
    " . "Reason: " . mysql_error());
    }
    else
    {
        $sql=mysql_query("select * from users where username = '".$_POST['username']."' and
          password = '".$_POST['password']."' ");
    
        if(mysql_num_rows > 0) 
       {
        $_SESSION['username'] = $_POST['username'];
        header("Location:interact.php");
       }
      else
      {
        echo "Invalid Username/password";
       }
    }
    
    ?>
    

    Login

    Username: Password:

    Interact.php

    Nexus | Envoy

    ->Slice the html and include header and footer file. ->Change the content in accordance with the url requested e.g. keep one file say index.php and include header then its content and lastly footer. ->say the request is index.php?content=register,then you will get the values of $_REQUEST['content'] and based on it you will include the content file ->like if $_REQUEST['content'] = 'register', include('register.php'). Register.php will contain the content only and no header and footer. ->If $_REQUEST['content'] is empty , show home page. Sample header.php: Sample footer.php Sample index.php
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题