2017-07-15 138 views
0

这是我的第一篇文章,所以请对评论进行温和处理。检查用户是否已登录,然后检查用户是否使用单选按钮进行了登录

我有一个网站我正在玩。它有一个基本的登录页面/注册页面。如果用户没有登录,他可以访问所有页面,但无法看到价格。当用户登录价格变得可用时。我的问题是我如何做到这一点,当用户在登录页面上时,他有一个选项可以选择两个单选按钮中的一个,并且选定的用户将被登录,并且将显示特定于任一单选按钮的内容。 。即:登录和高价格点击(一旦登录,我可以看到价格高)注销,点击登录与低价格检查,然后我看到只有低价格。

这里是我的login.php

<?php 
require('db.php'); 
// If form submitted, insert values into the database. 
if (isset($_POST['username'])){ 

    $username = stripslashes($_REQUEST['username']); // removes backslashes 
    $username = mysqli_real_escape_string($con,$username); //escapes special characters in a string 
    $password = stripslashes($_REQUEST['password']); 
    $password = mysqli_real_escape_string($con,$password); 

//Checking is user existing in the database or not 
    $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'"; 
    $result = mysqli_query($con,$query) or die(mysql_error()); 
    $rows = mysqli_num_rows($result); 
    if($rows==1){ 
     $_SESSION['username'] = $username; 
     header("Location: index.php"); // Redirect user to index.php 
     }else{ 
      echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login_page.php'>Login</a></div>"; 
      } 
}else{?> 

<!-- header ends here --> 
<!-- ****************************************** --> 
<!-- enquiry/newsletter/login/register goes here --> 
<div class="container"> 
</div> 
<div class="clear"></div> 
<!-- Main page content goes here --> 
<div class="content_landing"> 
<p><div class="form"> 
<h2>Log In to view Prices and Specials</h2> 
<form action="" method="post" name="login"> 
<input type="radio" name="radio" value="birthday" checked> Birthday Prices<br> 
<input type="radio" name="radio" value="corporate"> Corporate Prices<br> 
<input type="text" name="username" placeholder="Username" required /><br> 
<input type="password" name="password" placeholder="Password" required /><br> 
<input name="submit" type="submit" value="Login" /> 
</form> 
<p>Not registered yet? <a href='registration.php'>Register Here</a></p> 
</div></p> 
<?php } ?> 
</div> 
</div> 

代码,一旦用户登录,他看到这个内容(在products.php:(他应该去吧)

<p><b>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p> 

<?php if(isset($_SESSION['username'])){ ?> 
<p>Pricing below:</p> 
<p><a class="link" href="logout.php" style="text-decoration:none">Logout</a></p> 
<?php }else{ ?> 
    <a class="link" href="login_page.php" style="text-decoration:none">login</a> or <a class="link" href="registration_page.php" style="text-decoration:none">Register</a> 
<?php } ?> 

我仍然是一个网页设计的超级noobo,所以请原谅我的缩进和方法等

回答

0

您只是指定一个新的会话变量,您在显示定价时检查:

<?php 
require('db.php'); 
if (isset($_POST['username'])){ 
    $username = stripslashes($_REQUEST['username']); 
    $username = mysqli_real_escape_string($con,$username); 
    $password = stripslashes($_REQUEST['password']); 
    $password = mysqli_real_escape_string($con,$password); 
    $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'"; 
    $result = mysqli_query($con,$query) or die(mysql_error()); 
    $rows = mysqli_num_rows($result); 
    if($rows==1){ 
     $_SESSION['username'] = $username; 
     $_SESSION['view_type'] = (!empty($_POST['corporate']))? 'c' : 'b'; 
     header("Location: index.php"); 
     exit; 
    }else{ 
     echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login_page.php'>Login</a></div>"; 
    } 
} else { ?>...etc. 

要使用的变量,这将是明智的一个或两个功能,这样你就不会带着一帮if/else重复自己一遍又一遍:

# Checks if user is corporate 
function isCorporate() 
    { 
     return (isset($_SESSION['view_type']) && $_SESSION['view_type'] == 'c'); 
    } 
# Checks if user is birthday 
function isBirthday() 
    { 
     return (isset($_SESSION['view_type']) && $_SESSION['view_type'] == 'b'); 
    } 
# Checks if user is logged in at all 
function isLoggedIn() 
    { 
     return (!empty($_SESSION['username'])); 
    } 

所以,你会做这样的事情:决定价格视图时对$_SESSION['view_type']

# Logged In? 
if(isLoggedIn()) { 
    # What kind of login? 
    echo (isCorporate())? 'Higher value' : 'Lower value'; 
} 
else 
    # Not logged in... 
    echo 'Log in to see price!'; 

检查。

只有只有视图类型,你可以使用相同的形式与复选框,如果选中,将允许你的逻辑忽略用户名和密码字段,你可以有一个单独的形式与那些,你可以有一对夫妇风格<a>链接看起来像单选按钮等。有很多方法可以让会话创建视图,而无需完全登录用户。

夫妇方面的注意事项,你不应该逃避用户意见,你应该是绑定参数,而不是。通过绑定参数,这意味着您不会像现在这样将变量放入sql语句中。其次,你应该使用password_hash()/password_verify()来存储和检索密码哈希。 md5()不足以保证安全。在使用header()重定向后,最后添加exit,即使没有任何内容,它也会停止脚本的任何进一步执行,这是很好的习惯。

有关这些功能的说明,您只需将它们存储在包含文件中,然后使用require_once('myfunctions.php');将它们包括在页面的顶部。


编辑:


因此,基于从注释你的代码片段:

<?php 
if(isset($_SESSION['username'])){ ?> 
    <p>Pricing below:</p> 
    <?php if(isCorporate()) { ?> 
    <h1>$100</h1> 
    <?php } elseif(isBirthday()) { ?> 
    <h3>$50</h3> 
    <?php } else { ?> 
    <p><em>NOT SET YET</em></p> 
    <?php } ?> 
    <p><a class="link" href="logout.php" style="text-decoration:none">Logout</a></p> 
<?php } else { ?> 
    <a class="link" href="login_page.php" style="text-decoration:none">login</a> or <a class="link" href="registration_page.php" style="text-decoration:none">Register</a> 
<?php } ?> 
+0

@verjas我基本上希望用户无论登录查看企业或生日聚会价格或如果他们没有登录只是为了查看网页内容。一旦他们登录产品页面将显示企业价格或生日价格。我没有使用数据库来存储价格......他们在每个产品说明下的产品页面的ifset中。 –

+0

是的,那很好,那么我所展示的你将会工作。 – Rasclatt

+0

如果您希望他们只需点击一个按钮,就可以创建单独的表单,只需提交这两个选项即可设置视图类型。 – Rasclatt