只有登录用户使用php才能访问页面

时间:2015-07-18 20:19:21

标签: php login accessible

您好我想让只有登录用户才能访问的页面。我搜索了这个,但我找到的所有结果都没有帮助所以我需要知道我应该在哪里添加代码。 这是我的login.php:

[cell.textLabel setNumberOfLines:0];
[cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping]

我需要将 <?php session_start(); include_once 'dbconnect.php'; if(isset($_SESSION['user'])!="") { header("Location: home.php"); } if(isset($_POST['btn-login'])) { $email = mysql_real_escape_string($_POST['email']); $upass = mysql_real_escape_string($_POST['pass']); $res=mysql_query("SELECT * FROM users WHERE email='$email'"); $row=mysql_fetch_array($res); if($row['password']==md5($upass)) { $_SESSION['user'] = $row['user_id']; header("Location: home.php"); } else { ?> <script>alert('wrong details');</script> <?php } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ورود</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <center> <div id="login-form"> <form method="post"> <table align="center" width="30%" border="0"> <tr> <td><input type="text" name="email" placeholder="Your Email" required /></td> </tr> <tr> <td><input type="password" name="pass" placeholder="Your Password" required /></td> </tr> <tr> <td><button type="submit" name="btn-login">ورود</button></td> </tr> <tr> <td><a href="register.php">ثبت نام</a></td> </tr> </table> </form> </div> </center> </body> </html> 放到我拥有的每个页面吗?如果不是我应该把它放在哪里? 我应该把它放在哪里:

session_start();

tanq如果我需要为你们添加更多细节,请告诉我。

1 个答案:

答案 0 :(得分:0)

我认为你必须制作一个通用的头文件并放置像session_start();和ob_flush();在该文件中并将其包含在所有页面中。  所以你可以在任何地方访问它,而无需重写代码。重写相同的代码是坏习惯

相关问题