Reputation: 1970
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.
Upvotes: 0
Views: 54
Reputation: 44831
You have two problems: (1) file types and (2) your code.
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.
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.
Upvotes: 1