Log in

View Full Version : Dynamic Header (Login)



Fireodx
10-07-2010, 01:23 AM
Hi,

I'm not sure if this is the right place to post this message, but anyway, I need help with my login.

I have an header with a sign in button at the top right, and when the user clicks on the the button it directs them to the login page. When they insert their specific password and submit the form, they're taken back to the homepage.

What I'm trying to do is when they've logged on, I don't want anything but the sign in button in the header.php to change and I want to replace it with a sign out button.

This is a sample of what I currently have in my header.php. (I've put 'Hello' where the sign out button should be) (I haven't made the button yet).


<?php
require_once 'classes/membership.php';
?>
<div id="header">
<div class="header-left">
BANNER GOES HERE
</div>
<div class="header-right">
<div id="signin">
<?php
if(isset($_SESSION['pwd']) != '') {
print '<h1>Hello</h1>';
} else {
print '<a href="login.php"><img src="images/signin_button.jpg" border="0"></a>';
}
?>
</div>
</div>
</div>

fastsol1
10-07-2010, 01:35 AM
Does it not do this currently? The only thing I can see that might be an issue is that I probably wouldn't use the != '' in your if(). If the SESSION is set then it's not likely to be equal to nothing cause they have successfully logged in in order to set the SESSION['pwd'].

Fireodx
10-07-2010, 01:42 AM
No it doesn't seem to work....

I don't have the code in front of me atm, but I think either the homepage didn't load after the form was submitted or it had no effect....

I've tried to find tutorials or something, but I can't seem to find exactly what I want.

fileserverdirect
10-07-2010, 01:58 AM
Replace :
if(isset($_SESSION['pwd']) != '') {
with
if(isset($_SESSION['pwd'])) {

Fireodx
10-07-2010, 02:06 AM
Thanks.

I'm currently at work, when I get home I'll see if has any effect.

Is the code I used in the right format? I was just taking a guess when I was coding it last night.

If anyone here understands what I'm asking and you have a sample of code that is better than how I coded it, I would really appreciate it if you would share with me.

Cheers

Fireodx
10-07-2010, 09:29 AM
Okay I've fixed the segment of code but nothing has changed and it still doesn't work.

This is the code for all the files involved in my login.

I don't know why it doesn't work but I believe the problem is either with the header or membership.php files.

login.php


<?php

require_once 'classes/membership.php';
$membership = new membership();

// If the user clicks the "Log Out" link on the index page.
if(isset($_GET['status']) && $_GET['status'] == 'loggedout') {
$membership->log_user_out();
}

// Did the user enter a password and click submit?
if($_POST && !empty($_POST['pwd'])) {
$response = $membership->validate_user($_POST['pwd']);

} else if(isset($_POST['enter'])){
if($_POST && empty($_POST['pwd'])) {
$response2 = "Please enter a code.";
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/main.css"/>
<title>
Design Explosions &middot; Sign In
</title>
<script type="text/javascript" src="javascript/mootools.js"></script>
<script type="text/javascript" src="javascript/imageMenu.js"></script>
</head>

<body>
<div id="page-wrap">
<?php include ("header.php"); ?>
<div id="inside">
<?php include ("menu.php"); ?>
<script type="text/javascript">
window.addEvent('domready', function(){
var myMenu = new ImageMenu($$('#imageMenu a'),{openWidth:310, border:0, onOpen:function(e,i){window.location=(e);}});
});
</script>
<div id="main-body">
<form name="input" method="post" action="">
<div id="login_table_wrap">
<div id="login_title">
<h3>Account Sign In<h3><br/><small>Sign in using the form below and you'll be ready to upload and view your own artwork.</small>
</div>
<table>
<tr>
<td class="login_pass">
Code:
</td>
<td class="input">
<input type="password" name="pwd"/>
</td>
</tr>
<tr>
<td class="login_enter" colspan="2">
<input type="submit" value="Enter" name="enter" id="enter"/>
</td>
</tr>
</table>
</div>
</form>
<?php if(isset($response)) echo "<h4 class='alert'>" . $response . "</h4>"; ?>
<?php if(isset($response2)) echo "<h4 class='alert'>" . $response2 . "</h4>"; ?>
</div>
<?php include ("footer.php"); ?>
</div>
</div>
</body>
</html>
header.php


<?php

require_once 'classes/membership.php';

?>
<div id="header">
<div class="header-left">
BANNER GOES HERE
</div>
<div class="header-right">
<div id="signin">
<?php
if(isset($_SESSION['pwd'])) {
print '<a href="login.php?status=loggedout">Log Out</a>';
} else {
print '<a href="login.php"><img src="images/signin_button.jpg" border="0"></a>';
}
?>
</div>
</div>
</div>

usergallery.php


<?php

require_once 'classes/membership.php';
$membership = new membership();

$membership->confirm_member();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/main.css"/>
<title>
Design Explosions &middot; Your Gallery
</title>
<script type="text/javascript" src="javascript/mootools.js"></script>
<script type="text/javascript" src="javascript/imageMenu.js"></script>
</head>

<body>
<div id="page-wrap">
<?php include("header.php"); ?>
<div id="inside">
<?php include("menu.php"); ?>
<script type="text/javascript">
window.addEvent('domready', function(){
var myMenu = new ImageMenu($$('#imageMenu a'),{openWidth:310, border:0, onOpen:function(e,i){window.location=(e);}});
});
</script>
<div id="main-body">
<div id="user_image_gallery">
IMAGE GALLERY
<a href="login.php?status=loggedout">Log Out</a>
</div>
</div>
<?php include ("footer.php"); ?>
</div>
</div>
</body>
</html>
membership.php


<?php

session_start();
require 'classes/mysql.php';

class membership {

function validate_user($pwd) {
$mysql = new mysql();
$ensure_credentials = $mysql->verify_pwd($pwd);

if($ensure_credentials) {
$_SESSION['status'] = 'authorized';
header("location: user_gallery.php");
} else return "Sorry that code does not exist.";
}

function confirm_member() {
if($_SESSION['status'] !='authorized') header("location: login.php");
}

function log_user_out() {
if(isset($_SESSION['status'])) {
unset($_SESSION['status']);

if(isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time() - 1000);
session_destroy();
}
}
}

mysql.php


<?php

require_once 'includes/constants.php';

class mysql {

private $conn;

function __construct() {
$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or
die('There was a problem connecting to the database.');
}

function verify_pwd($pwd) {

$query = "SELECT *
FROM users
WHERE password = ?
LIMIT 1";

if($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('s', $pwd);
$stmt->execute();

if($stmt->fetch()) {
$stmt->close();
return true;
}
}
}
}

constants.php


<?php

/**
* Databse Constants - these constants are required
* in order for there to be a successful conection
* to the MySQL database.
*/

define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'membership');

?>

Fireodx
10-07-2010, 09:37 AM
Everything with the login works fine.

The user can log in.
If the user incorrectly types the password they get an error message.
If the user types nothing they get an error message.

The user can log out.
The user cannot access the user gallery without logging in first.
If the user doesn't logout they remain signed in until they close the site or logout.

I just need the sign in button to be replaced with a sign out button, but I don't know what to do to make it work...

djr33
10-07-2010, 03:25 PM
I believe all you need to do is use a simple if statement based on the 'status' variable.

fileserverdirect
10-07-2010, 07:32 PM
Umm, it looks like you have the code all done. I'm not sure what the problem is. Is the logout link not showing up? Is it not logging out when you click the link? Is there an error message?
Please provide more information to better help us understand the problem.

Axander
10-07-2010, 09:52 PM
I believe all you need to do is use a simple if statement based on the 'status' variable.

Further to the above, in membership.php you are setting $_SESSION['status'] = 'authorized'; but in the header you are checking $_SESSION['pwd'].

If you change the if statement to use the "status" variable, as mentioned by djr33, then it should work.

Fireodx
10-08-2010, 02:45 AM
Hi,

I'll check if the solution works when I get back home.

But, I got another question involving my code.

I currently have it connecting to a database on phpmyadmin, but everytime I hop onto a new computer I have to re-create the database.

With the code I have, what is the quickest way to connect and create a database called membership, create a table called users and add values (id, int, auto_increment, primary key) and (password, varchar(45)).

I've tried to do it several times, but I couldn't get it working. The only way I can think of doing this is to wipe the mysql.php and start again. However, I don't have much time left to complete this website and I would rather find a way to just implement it into my current code.

Oh and the file that is currently connecting the database is mysql.php.

Thanks in advance.

Fireodx
10-08-2010, 09:04 AM
Okay I have changed it to 'status' and it works. Thanks!

Sadly it doesn't work when I have an image as the button, strange. No worries, I can style the link to look like my button.

With my other question, is there an easy way to create a database, table, values without altering my current code. I've attempted several times already, but I always end up with a blank webpage.

Cheers!