Session if else语句

时间:2016-07-18 14:41:05

标签: php html session isset

我正在尝试显示信息A如果以用户身份登录,如果以管理员身份登录则显示信息B.这里的问题是,我只能执行第一个if statementelse statement,这意味着我可以登录。但是,它永远不会进入else if statement。任何人都可以帮助我,我不知道这里有什么问题。

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbdatabase = "test";
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
?>

somewhere.php

<?php
if(!isset($_SESSION)){
    session_start();
}
include("configuration.php");
?>
<html>
<body>
<?php

    if (isset($_SESSION['SESS_EXIST']) == true && isset($_SESSION['SESS_TYPE']) == 'A' ){ ?>
            //this is the user html information form
        <?php } 
    else if (isset($_SESSION['SESS_EXIST']) == true && isset($_SESSION['SESS_TYPE1']) == 'B' ){ ?>
            //this is the admin html information form
    <?php } else { ?>
            //ask user/admin to login html form
            <form action ="login.php">
               <button type="submit" name="login" class="btn-primary">Sign Up</button>
            </form>
    <?php } ?>
</body>
</html>

的login.php

<?php
session_start();
require("configuration.php");
if(isset($_SESSION['SESS_EXIST']) == TRUE) {
header("Location: somewhere.php");
die();
}
$email = $_POST['Email'];
$pass =  $_POST['Pass'];

$sql = "SELECT * FROM user WHERE Email='$email' and Pass ='$pass' " ;
$res = mysql_query($sql);
$rows = mysql_num_rows($res);

$sql1 = "SELECT * FROM admin WHERE Email='$email' and Pass ='$pass' " ;
$res1 = mysql_query($sql1);
$rows1 = mysql_num_rows($res1);

if($rows == 1)
{
$row = mysql_fetch_assoc($res);
$_SESSION['SESS_EMAIL'] = $row['Email'];
$_SESSION['SESS_NAME'] = $row['Name'];
$_SESSION['SESS_PASS'] = $row['Pass'];
$_SESSION['SESS_TYPE'] = 'A';
$_SESSION['SESS_LOGGED'] = 1;
header("Location: somewhere.php");
die();

}
else if($rows1 == 1)
{
$row1 = mysql_fetch_assoc($res1);
$_SESSION['SESS_EMAIL1'] = $row['Email'];
$_SESSION['SESS_NAME1'] = $row['Name'];
$_SESSION['SESS_PASS1'] = $row['Pass'];
$_SESSION['SESS_TYPE1'] = 'B';
$_SESSION['SESS_LOGGED'] = 1;
header("Location: somewhere.php");
die();
}
else {
echo '<script language = "javascript">';
echo 'alert("Fail login")';
echo '</script>';
echo "<script>window.location.assign('somewhere.php')</script>";
die();
}
?>

2 个答案:

答案 0 :(得分:1)

你的情况永远不会成真

if (isset($_SESSION['SESS_TYPE']) && $_SESSION['SESS_TYPE'] == 'A') {
   // your   code here
}


  `isset` — Determine if a `variable` is set and is not `NULL`. `Returns` `TRUE` if var exists and has value other than NULL, FALSE otherwise. 

答案 1 :(得分:1)

我认为您在退出时不会重置您的会话。试试这个:
的login.php

    Public Class Form1

    Private myCoins As New List(Of Coin)
    Private BagWeights As New Dictionary(Of String, Double)
    Private CoinWeights As New Dictionary(Of String, Double)
    Private CoinsInBag As New Dictionary(Of String, Integer)
    Private ActualNumberOfCoinsInBag As Integer

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim cb As ComboBox = DirectCast(sender, ComboBox)
        Dim TotalWeight As Double
        Dim txt As String = cb.Text
        If Not String.IsNullOrEmpty(txtTotalWeight.Text) Then
            TotalWeight = CDbl(txtTotalWeight.Text)
        Else
            MessageBox.Show("You Must enter a weight of Coins")
                    Exit Sub  'no point in continuing if there is no weight
        End If


        If ValidateWeightAdded(TotalWeight, CoinWeights.Item(cb.Text)) Then

            Dim CorrectNumber As Integer = CoinsInBag.Item(cb.Text)
            Dim AddRemove As Integer = CorrectNumber - ActualNumberOfCoinsInBag
            MessageBox.Show(AddRemove.ToString)

        Else
            MessageBox.Show("You have added an incorrect weight or some of your coins are fakes")

        End If


    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        SetupCoinsList()

        For Each c In myCoins
            ComboBox1.Items.Add(c.Name)
            CoinWeights.Add(c.Name, c.WeightOfCoinInGrammes)
            CoinsInBag.Add(c.Name, c.NumberAllowedInCoinBag)
            BagWeights.Add(c.Name, c.WeightOfCoinInGrammes * c.NumberAllowedInCoinBag)
        Next

    End Sub

    Private Sub SetupCoinsList()
        Dim onePenny As New Coin With {.Name = "OnePenny", .NumberAllowedInCoinBag = 100, .WeightOfCoinInGrammes = 3.56}
        Dim twoPenny As New Coin With {.Name = "TwoPence", .NumberAllowedInCoinBag = 50, .WeightOfCoinInGrammes = 7.12}
        Dim fivePenny As New Coin With {.Name = "FivePence", .NumberAllowedInCoinBag = 100, .WeightOfCoinInGrammes = 3.25}
        Dim tenPenny As New Coin With {.Name = "TenPence", .NumberAllowedInCoinBag = 50, .WeightOfCoinInGrammes = 6.5}
        Dim twentyPenny As New Coin With {.Name = "TwentyPence", .NumberAllowedInCoinBag = 50, .WeightOfCoinInGrammes = 5.0}
        Dim fiftyPenny As New Coin With {.Name = "FiftyPence", .NumberAllowedInCoinBag = 20, .WeightOfCoinInGrammes = 8.0}
        Dim onePound As New Coin With {.Name = "OnePound", .NumberAllowedInCoinBag = 20, .WeightOfCoinInGrammes = 9.5}
        Dim twoPound As New Coin With {.Name = "TwoPound", .NumberAllowedInCoinBag = 10, .WeightOfCoinInGrammes = 12.0}
        myCoins.Add(onePenny)
        myCoins.Add(twoPenny)
        myCoins.Add(fivePenny)
        myCoins.Add(tenPenny)
        myCoins.Add(twentyPenny)
        myCoins.Add(fiftyPenny)
        myCoins.Add(onePound)
        myCoins.Add(twoPound)
    End Sub

    Private Function ValidateWeightAdded(ByVal totalCoinWeight As Double, ByVal coinWeight As Double) As Boolean
'Essentially this just determines if the total weight of the coins is exactly divisible by the weight of the selected coin type.
        Dim myResult As Double = totalCoinWeight / coinWeight
        If myResult = (Fix(myResult)) Then
            ActualNumberOfCoinsInBag = CInt(myResult)
        End If
        Return myResult = (Fix(myResult))
    End Function
End Class

Public Class Coin

    Property Name As String

    Property NumberAllowedInCoinBag As Integer

    Property WeightOfCoinInGrammes As Double



    Public Sub New()
    End Sub


End Class

您的If语句也不正确。如其他答案中所述,它应该是这样的:
somewhere.php 编辑

<?php
session_start();
require("configuration.php");
if(isset($_SESSION['SESS_EXIST']) == TRUE) {
header("Location: somewhere.php");
die();
}
session_unset();
$email = $_POST['Email'];
$pass =  $_POST['Pass'];

$sql = "SELECT * FROM user WHERE Email='$email' and Pass ='$pass' " ;
$res = mysql_query($sql);
$rows = mysql_num_rows($res);

$sql1 = "SELECT * FROM admin WHERE Email='$email' and Pass ='$pass' " ;
$res1 = mysql_query($sql1);
$rows1 = mysql_num_rows($res1);

if($rows == 1)
{
$row = mysql_fetch_assoc($res);
$_SESSION['SESS_EMAIL'] = $row['Email'];
$_SESSION['SESS_NAME'] = $row['Name'];
$_SESSION['SESS_PASS'] = $row['Pass'];
$_SESSION['SESS_TYPE'] = 'A';
$_SESSION['SESS_LOGGED'] = 1;
header("Location: somewhere.php");
die();

}
else if($rows1 == 1)
{
$row1 = mysql_fetch_assoc($res1);
$_SESSION['SESS_EMAIL1'] = $row['Email'];
$_SESSION['SESS_NAME1'] = $row['Name'];
$_SESSION['SESS_PASS1'] = $row['Pass'];
$_SESSION['SESS_TYPE1'] = 'B';
$_SESSION['SESS_LOGGED'] = 1;
header("Location: somewhere.php");
die();
}
else {
echo '<script language = "javascript">';
echo 'alert("Fail login")';
echo '</script>';
echo "<script>window.location.assign('somewhere.php')</script>";
die();
}
?>