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. Even in refreshing that page, it will disappear.
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
////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
<?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. It will work right there but then moving to my next admin link it won't. I had the echo $_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. 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(); 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.
Double Edit - Was going to encrypt passwords. Would there be a better way to accomplish things without $_SESSION variables? What if only the $_SESSION['username'] was set but not a password one?
FINAL- Okay thanks everyone! Got it and am revamping things.
session_start()
to the index page and every page you expect to use session variables. You're checking for session variables without starting the session. – Jay Blanchardprint_r($_SESSION); exit;
in order to see the status of the SESSION variable. If it's not filled with your values, you might have forgotten to start the session as @JayBlanchard said, or you're unsetting the variables somewhere along. – James Spence