Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP+;MySQL-如何在继续签出之前检查是否已登录_Php_Mysql - Fatal编程技术网

PHP+;MySQL-如何在继续签出之前检查是否已登录

PHP+;MySQL-如何在继续签出之前检查是否已登录,php,mysql,Php,Mysql,我正在做一个学校项目,我必须创建一个网站。我对php不是很在行,觉得它很让人困惑。我需要做到的是,当客户点击“立即购买”按钮并输入他们的电子邮件地址时,页面将显示1。只有在检查他们是否已登录后,才能将他们的订单写入数据库。如果客户未登录,他们将被重定向到登录页面 <?php // Start a PHP session session_start(); // Check to see if user is already logged in if(iss

我正在做一个学校项目,我必须创建一个网站。我对php不是很在行,觉得它很让人困惑。我需要做到的是,当客户点击“立即购买”按钮并输入他们的电子邮件地址时,页面将显示1。只有在检查他们是否已登录后,才能将他们的订单写入数据库。如果客户未登录,他们将被重定向到登录页面

<?php
    //  Start a PHP session
    session_start();

    // Check to see if user is already logged in
    if(isset($_SESSION["sname"]))
    {
        header('Location: custwelcome.php');
        exit;
    }

?>

我假设,这应该确保从客户登录中存储的会话变量存在

我会将这段代码放在将客户订单写入数据库的php代码之前吗?或者我会把它放在“产品”的详细页面上。(对于作业,网页在点击“立即购买”按钮之前还是之后进行检查并不重要,只需进行检查即可)

如果这没有提供足够的详细信息,以下是我的产品页面:

<!DOCTYPE html>

<!-- XXXX 
project
athletic1.htm -->

<html lang="en">

  <head>
    <!-- Meta tag -->
    <meta name="robots" content="noindex.nofollow" />
    <meta charset="utf-8" />

    <!-- Link tag for CSS -->
    <link type="text/css" rel="stylesheet" href="../stylesheet/project.css" />

    <!-- Javascript tags -->
    <script type="text/javascript" src="../js/projectmessages.js"></script>


    <!-- Web Page Title -->
    <title>Shoe Source Unlimited - Athletic Shoe Sale</title>

  </head>

  <body>
    <div id="header">

                <img src="../images/logo.png" alt="Logo" />

    <p class="sh1">Shoe Source Unlimited</p>
    <p class="sh2">Your source for lightning sales of this season's hot shoes!    </p>

        <p class="sh3">XXXXX </p>
  </div>

    <div id="navbar">
        <ul id="nav">
            <li>
                <a href="../homepage.htm">Home</a>                          
            </li>

            <li>
                <a href="#">Men's</a>
                    <ul>
                        <li><a href="../sneakers.htm">Sneakers</a></li>
                        <li><a href="../loafers.htm">Loafers</a></li>
                        <li><a href="../athletic.htm">Athletic</a></li>
                    </ul>                       
            </li>

            <li>
                <a href="#">Women's</a>
                    <ul>
                        <li><a href="../boots.htm">Boots</a></li>
                        <li><a href="../heels.htm">Heels</a></li>
                        <li><a href="../sandals.htm">Sandals</a></li>       
                    </ul>               
            </li>

            <li>
                <a href="../about.htm">About Us</a>         

            </li>

            <li>
                <a href="../signup.htm">Sign Up</a>

            </li>

            <li>
                <a href="../login.php">Log In</a>

            </li>

        </ul>   
    </div>




  <div id="external">

    <p>

            <a href="https://twitter.com/XXXXX" onclick="window.open(this.href); return false;">
                <img src="../images/twitter.jpg" alt="twitter" />
            </a>
     </p>
            <p>Follow us on Twitter!</p>
        <br/>


    <p>

            <a href="http://www.facebook.com/ShoeSourceUnlimited" onclick="window.open(this.href); return false;">
                <img src="../images/facebook.png" alt="facebook" />
            </a>
    </p>
            <p>Like us on Facebook!</p>
              <br/>


        <a href="../em/projectem.htm">
        <img src="../images/email.jpg" alt="pinkemail"  />
        </a>
    </p>    
            <p> Send us an email!</p>   
    </div>  




<form id="joinform" action="../purchaseconfirm.php" method="post">
    <div id="about">

        <p class="abouttitle">Grey Athletic Shoe with Orange Enhancements</p>
        <p class="abouttitle"><img src="../images/shoes/athletic1.jpg" alt="athletic1" /></p>
        <p class="description">This low-top athletic shoe is designed for comfort during long-use.
    The bright orange color is just bright enough to show some flash without going over the top!</p>

    <p class="price">Price: $22.00 - tax included </p><br/><br/>    

        <!--Email -->
                <p class="size">
                <label for="email">Email:</label>
                <input type="email" id="email" name="email" required
                title="Email: 6-59 characters, lowercase, valid email     only!"
                pattern="[a-z0-9.-_]+@[a-z0-9-]+\.[a-z]{2,6}"
                maxlength="60"
                onfocus="emailmsg()" />
                </p>

        <!-- Pick a size -->

                <select name="size" id="size" required title="Select a size" >
                <option value="">Select a size...</option>
                <option value="athletic1size10">Grey and Orange Mens 10</option>
                <option value="athletic1size10.5">Grey and Orange Mens 10.5</option>
                </select>   


        <p class="submit">
                <input type="submit" 
                value=" Buy Now! "/>
            <br/><br/>
        </p>        
    </div>

</form>





    <div id="footer">

        <p>

            &copy;2014, XXXXX

        </p>
    </div>

  </body>

</html> 

鞋源无限-运动鞋销售
鞋源无限

本季热门鞋的闪电销售来源

XXXXX

在Twitter上关注我们


就像我们在Facebook上


给我们发电子邮件

带有橙色配件的灰色运动鞋

这款低帮运动鞋专为长时间使用而设计。 明亮的橙色刚好足够亮,可以显示一些闪光,而不会超过顶部

价格:22.00美元-含税

电邮:

选择一个尺寸。。。 灰色和橙色男式10码 灰色和橙色男式10.5码



&抄袭;2014年,XXXXX

这是我所有购买的“确认页”:

<!--  
project
purchaseconfirm.php -->

<!-- this will write to DB -->



<?php
// Connect to LOCAL or SERVER MySQL Database. Just change between local and server
    include('connect/local-connect.php');

// initialize and populate PHP variables from user-entered data

    $email = $_POST['email'];
    $size = $_POST['size'];



//Build a MySQL statement to populate the database table
    $query =
    "INSERT INTO shopping (email, size)
    VALUES ('$email', '$size')";

// Run the query we just built
     $result = mysqli_query($dbc,$query) or die('Unable to write to database');

// Close the database connection
    mysqli_close($dbc);

?>






<html lang="en">

  <head>
    <!-- Meta tag -->
    <meta name="robots" content="noindex.nofollow" />
    <meta charset="utf-8" />

    <!-- Link tag for CSS -->
    <link type="text/css" rel="stylesheet" href="stylesheet/project.css" />

    <!-- Javascript tags -->
    <script type="text/javascript" src="js/projectmessages.js"></script>


    <!-- Web Page Title -->
    <title>Shoe Source Unlimited - Purchase Confirmation</title>

  </head>

   <body>
     <div id="header">

                <img src="images/logo.png" alt="Logo" />

        <p class="sh1">Shoe Source Unlimited</p>
        <p class="sh2">Your source for lightning sales of this season's hot shoes!</p>

        <p class="sh3">XXXXX</p>
  </div>

    <div id="navbar">
         <ul id="nav">
            <li>
                <a href="homepage.htm">Home</a>                         
            </li>

            <li>
                <a href="#">Men's</a>
                    <ul>
                         <li><a href="sneakers.htm">Sneakers</a></li>
                        <li><a href="loafers.htm">Loafers</a></li>
                        <li><a href="athletic.htm">Athletic</a></li>
                    </ul>                       
            </li>

            <li>
                <a href="#">Women's</a>
                    <ul>
                        <li><a href="boots.htm">Boots</a></li>
                        <li><a href="heels.htm">Heels</a></li>
                        <li><a href="sandals.htm">Sandals</a></li>      
                     </ul>              
            </li>

            <li>
                <a href="about.htm">About Us</a>            

            </li>

            <li>
                <a href="signup.htm">Sign Up</a>

            </li>

            <li> 
                <a href="login/login.php">Log In</a>

            </li>

        </ul>   
    </div>


  <div id="external">

    <p>

            <a href="https://twitter.com/XXXXX" onclick="window.open(this.href); return false;">
                <img src="images/twitter.jpg" alt="twitter" />
            </a>
    </p>
            <p>Follow us on Twitter!</p>
        <br/>


    <p>

            <a href="http://www.facebook.com/ShoeSourceUnlimited" onclick="window.open(this.href); return false;">
                <img src="images/facebook.png" alt="facebook" />
            </a>
    </p>
            <p>Like us on Facebook!</p>
        <br/>


        <a href="email/projectem.htm">
         <img src="images/email.jpg" alt="pinkemail"    />
        </a>

            <p> Send us an email! </p>  
    </div>      

    <div id="main">
    <p>Purchase Confirmed!</p>

    </div>

    <div id="about">
       <p class="bold">


        </p>

        <p> Your payment has been approved and your purchase shall be shipped to     you shortly </p>
        <br/>
        <p> Feel free to contact us with any further questions.</p>
        <br/>
<form id="joinform" action="homepage.htm" method="get">
        <p class="submit">
                <input type="submit" 
                value=" Home Page "/>

    </div>







    <div id="footer">

        <p>

            &copy;2014, XXXXX

        </p>
    </div>

  </body>

</html>


你可以像我一样

if (!isset($_SESSION))
  {
    session_start();
  }

// Check to see if user is already logged in
    if(isset($_SESSION["sname"]))
    {
        header('Location: custwelcome.php');
    }

每当您觉得用户必须先登录才能执行某些私人作业时,您必须首先检查用户是否已登录。我在第一个“代码框”中发布的php代码是否足够?因为它正在检查会话变量,对吗?请检查我的答案