Hello.
I would consider myself brand new to PHP and programming in general. So please be explicit in your posts if you can in your explanations. I do well with logic and reasoning.
Ok, on to my goal, problem, and question.
I am currently attempting to code a website where visitors are able to post their own media, news bits, as well as browse those posted by others. These pages are protected by a relatively simple login script that I had simply looked over, typed into Notepad++ myself, and modified to work with my own environment.
I wish for every page that a user is logged into to display their own username near the top of the webpage. The problem is, I don’t know exactly how to implement such a feature using the existing login script that I have.
I have read around that you can use sessions or cookies to store variables for later retrieval in PHP. (The only difference between the two is when the data expires). I’d rather use sessions if possible.
Here is the login code that I am using on the site:
[php]
//set the login session storing their id - we use this to see if they are logged in or not for users Online.
$_SESSION['uid'] = $row['id'];
//show message
echo "<center>You have successfully logged in. Continue.</center>";
//update the online field to 50 seconds into the future
$time = date('U')+50;
mysql_query("UPDATE `usertable` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
//redirect them to the home page
header("Location:homepage.php");
}
}
}
}
}
?>
<form action="login.php" method="post">
<div id="border">
<table cellpadding="2" cellspacing="0" border="0">
<tr>
<td>Username:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Login" /></td>
</tr>
<tr>
<td align="center" colspan="2"><a href="register.php">Registration</a> | <a href="forgot.php">Forgot Password?</a></td>
</tr>
<tr>
<td align="center" colspan="2"><a href="newactive.php">No Activation Email?</a> | <a href="dd-home.php">Back to Home</a></td>
</tr>
</table>
</div>
</form>
</body>
<?
ob_end_flush();
?>
[/php]
The code below is what’s on homepage.php . Note that this is not the complete code; only the php parts are included.
[php]
<?php //Start Session to retrieve session variables (Doesn't work, Coding issue?) session_start() ?>… [ To Next Section of PHP ] …
<?php
//Check to see if a session does exist
if(strcmp($_SESSION['uid'],"") == 1)
{ //If the session does, display the username of the user logged in.
echo "The $username var does not work!
View Users | Logout";
}
else
{ //If the session does NOT, display notification and LOGIN link.
echo "You are not logged in!
Login Here | Registration";
}
?>
There has to be some way of capturing the $username variable and storing it into the same session along with the login check when the user logs in and is redirected to homepage.php. That way I can recall it using $_SESSION[‘username’] = $username.