dongxing4805 2015-09-30 18:34
浏览 47
已采纳

主页不会显示用户名

I am working on my website and, for the life of me, can't seem to get the username to pop up on my index.html page after it redirects a user to the page after they log in.

connectivity.php

function SignIn() 
{ 
    session_start(); //starting the session for user profile page 
    if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text 
    { 
        $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); 
        $row = mysql_fetch_array($query) or die(mysql_error());
        if(!empty($row['userName']) AND !empty($row['pass'])) 
        { 
            $_SESSION['userName'] = $row['pass']; 
            header("location: http://www.gotospectrum.com/index.html");

        } else { 
            echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY..."; 
            } 
        } 
    } 
    if(isset($_POST['submit'])) 
    { 
        SignIn(); 
    } 

?>

index.html

<?php 
    session_start();session_destroy();
    session_start();
 ?>
<!DOCTYPE html>
<html lang="en">



<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li>
                        <a href="about.html">About</a>
                    </li>
                    <li>
                        <a href="Services.html">Services</a>
                    </li>
                    <li>
                        <a href="index.php">Contact</a>
                    </li>

                    <li>
                        <a href="Login/Sign-In.html">Login</a>
                    </li>

                    <li>
                        <a href="/Login/logout.php">Logout</a>
                    </li>

                    <li>
                        <?php 
                            echo isset($_SESSION['userName'];

                        ?>
                    </li>  

I'm calling the session at the top as you can see and I'm trying to echo the username in the <li> after Logout, I'll move it later, but at this point I'm just trying to get it to work.

I have been at this now for hours and it's really frustrating me.

  • 写回答

1条回答 默认 最新

  • duanbei2914 2015-09-30 18:36
    关注

    You have two problems: (1) file types and (2) your code.

    The File Type Problem

    By default, .html is processed by your server as HTML - it never goes through the PHP engine. Change your file name to index.php.

    You can verify this is the problem by viewing the source of index.html when you load it in a browser. You will see your unprocessed PHP code, which obviously is not what you want.

    As an alternative, if you are using Apache, you can force your server to process HTML files as PHP by adding this line to a .htaccess file in the root directory of your site: AddType application/x-httpd-php .htm .html. See this answer. This is not the recommended way to fix the problem, however, as it adds a lot of server load for serving up even a basic .html file.

    Note: some servers are not setup correctly to treat index.php as the default file. If you get a 404 error after renaming your file and trying to load your homepage, check your settings for default page names.

    Your Code Problems

    You call session_destroy(); in the first line of your PHP code, which means $_SESSION['userName'] will never be set when you get to that part of the code. Take out session_destroy();.

    Also, you are missing the closing ) in echo isset($_SESSION['userName'];:

    echo isset($_SESSION['userName']);
    

    With regards to this last error: that is a syntax error that will cause a "white screen of death." You really should use an IDE that will catch these kinds of things. I'm personally partial to PHPStorm, but any decent IDE should catch a syntax error like that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解