Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Dynamic Header (Login)

  1. #1
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Dynamic Header (Login)

    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).

    Code:
    <?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>

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    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'].

  3. #3
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Replace :
    if(isset($_SESSION['pwd']) != '') {
    with
    if(isset($_SESSION['pwd'])) {
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  5. #5
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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

    Code:
    <?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

    Code:
    <?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

    Code:
    <?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

    Code:
    <?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

    Code:
    <?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

    Code:
    <?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');
    
    ?>

  7. #7
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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...

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I believe all you need to do is use a simple if statement based on the 'status' variable.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. The Following User Says Thank You to djr33 For This Useful Post:

    Fireodx (10-08-2010)

  10. #9
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    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.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  11. #10
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Smile

    Quote Originally Posted by djr33 View Post
    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.

  12. The Following User Says Thank You to Axander For This Useful Post:

    Fireodx (10-08-2010)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •