简体   繁体   English

PHP $ _SESSION变量不成立

[英]PHP $_SESSION variables not holding

I'm not great at this stuff as will be evident, but would like to try and get better. 显而易见,我并不擅长于此,但想尝试变得更好。 I have a wedding website for my fiancé and I, that I'm having troubles with. 我和我的未婚夫都有一个婚礼网站,很麻烦。 I have a simple login on the index that goes through a login page, login confirmation page and back to the index. 我在索引上有一个简单的登录名,该登录名通过登录页面,登录确认页面并返回索引。 It will hold the $_SESSION['name' variable from the form, confirmed in mysql, back to the index but not to anywhere else. 它将保存表单中的$ _SESSION ['name'变量,该变量在mysql中确认,返回索引,但不返回其他任何地方。 Even in refreshing that page, it will disappear. 即使刷新该页面,它也会消失。

index.php index.php

  <div id="login">
        <span class="login">
            <form action="login.php" method="post" id="loginform">
                <label for="name">User:</label>
                <input type="text" name="name" id="name" size="12">
                <label for="password">Password:</label>
                <input type="password" name="password" id="password" size="12">
                <input type="submit" value="submit" id="submit">
                <input type="button" value="hide" id="hide" onclick="loghide()">
            </form>
        </span>
    </div>
    <br />
    <div id="logged">
        <?php
if (isset($_SESSION['username'])){
echo "<script      type='text/javascript'>document.getElementById('logged').style.cssText='opacity:1;-webkit-transition:1s;-moz-transition:1s;-o-transition:1s;-ms-transition:1s;transition:1s;';</script>";
echo "<script    type='text/javascript'>document.getElementById('login').style.cssText='display:none;';</script>";
}      
?>
        <span class="logged">
                <ul id="adminlinks">
                    <div id="event">
                        <div id="hiddenlinks">
                            <li><a class="links" id="hidden2"     href="events.html">index</a></li>
                            <br />
                            <li><a class="links" id="hidden3"      href="events.html">our story</a></li>
                            <br />
                        </div>
                        <li><a class="links">update</a></li>
                    </div>
                    <li><a class="links" href="addphotos.php">add     photos</a></li>
                       <li><a class="links" href="<?php session_destroy(); header("Location:index.php"); ?>">Log Out</a></li>
                </ul>
        </span>
    </div>
</div>
<div id="bottom">
    <span class="foot">july fourth, two thousand and fifteen</span>
</div>
</div>

<?php
echo $_SESSION['username'];
echo $myusername;
?>

login.php login.php

////deleted mysql connection stuff
session_start();
$_SESSION['username']=$myusername;
$_SESSION['password']=$mypassword; 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

login_success.php login_success.php

<?php
session_start();
if (!isset($_SESSION['username'])){
echo "nope";
} else {header("location:index.php");}

?>

I have session_start()'s at the tops of those pages swell. 我的session_start()位于这些页面顶部。 It will work right there but then moving to my next admin link it won't. 它会在那里工作,但是然后转到我的下一个管理员链接就行了。 I had the echo $_SESSION['username']; 我有回声$ _SESSION ['username']; at the bottom to see if it was registered. 在底部查看它是否已注册。

Its hosted on GoDaddy which does save session stuff in a root/tmp folder so there is a path for it. 它托管在GoDaddy上,它确实将会话内容保存在root / tmp文件夹中,因此有一个路径。 I'm at a loss. 我很茫然。

Any ideas for a noob? 对菜鸟有什么想法吗?

It's not an important login or anything as it would just be me or my fiancé logging in to update pictures or something but wanted to do it myself and make it nice. 这不是重要的登录信息,也不是什么,因为它只是我或我的未婚夫登录以更新图片或其他内容,但我想自己做并使其美观。

Thanks, Matt 谢谢,马特

Edit- Index.php and addphotos.php have session_start(); 编辑-Index.php和addphotos.php具有session_start(); at the top of the page. 在页面顶部。

the next admin link as I called it is addphotos.php basically the same as the index with other content to be added later. 我所谓的下一个管理链接是addphotos.php,它与索引和其他要添加的内容基本相同。

Double Edit - Was going to encrypt passwords. 双重编辑-将要加密密码。 Would there be a better way to accomplish things without $_SESSION variables? 没有$ _SESSION变量,会有更好的方法来完成任务吗? What if only the $_SESSION['username'] was set but not a password one? 如果仅设置了$ _SESSION ['username']而不设置了密码,该怎么办?

FINAL- Okay thanks everyone! 最后-谢谢大家! Got it and am revamping things. 明白了,正在改头换面。

<a class="links" href="<?php session_destroy(); header("Location:index.php");?>">Log Out</a>

This is not creating a link to this code, you're actually running it! 这不是创建此代码的链接,实际上是在运行它! Create a logout.php page with this code in it, and link to that page instead. 创建一个包含此代码的logout.php页面,然后链接到该页面。 This is why your session is being destroyed. 这就是为什么您的会话被破坏的原因。 You also need session_start() on all pages that use sessions. 您还需要在所有使用会话的页面上使用session_start()

You must call session_start(); 您必须调用session_start(); on the index page in order to use it there, the sessions are kept, it's just that you need to tell the program to start the session and so it will use the values found there. 为了在此处使用索引,将保留会话,只是您需要告诉程序开始会话,因此它将使用在那里找到的值。 put the following code on the first line of your index.php file: 将以下代码放在index.php文件的第一行:

<?php session_start(); ?>

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM