+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Some PHP Help

  1. #1
    Jober68 is offline x10Hosting Member Jober68 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    62

    Some PHP Help

    I'm designing a website and I really need some help with php. I have a login script however I can't make it log out like I want it to. I also want to be able to have a type of admin page, which would require a user to log in to see, where there would be a form that would ask for a title and content and then when they submit that it displays on the index page. If you think you could help me out with this please e-mail me at joemeyer@christian-debate.com. I will give all my credits to anyone who can successfully solve my problems.

  2. #2
    DeadBattery's Avatar
    DeadBattery is offline Community Support Team DeadBattery is a name known to allDeadBattery is a name known to all
    Join Date
    Mar 2008
    Location
    localhost
    Posts
    4,019

    Re: Some PHP Help

    Are you using Sessions or Cookies?
    To destroy a cookie, use this code:
    PHP Code:
    <?php 
    // set the expiration date to one hour ago.  you might need to change the time to however long you set it to.
    setcookie("cookie_name"""time()-3600);
    ?>
    If you are using sessions, you can use this:
    PHP Code:
    <?php
    unset($_SESSION['session_name']);
    ?>
    About the admin page, you might want to add a column `type` to your members table (assuming you are using MySQL). You could put 'u' in for a regular user and 'a' in for administrator.
    When someone logs in, you could set a cookie with their username and their password (encrypted of course ;) ) and check to see if that user has permission to view the AdminCP by their type.


  3. #3
    Jober68 is offline x10Hosting Member Jober68 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    62

    Re: Some PHP Help

    Ok the only problem... I have ended the session cookie however... I have all the login information on my main page... I have a members.php which says "Welcome (username). You are logged in" And my login script. However, once I've logged in I can't make it change on my main page back to the login script. I guess I just don't know how to make it re-check if the session cookie is still active? Not sure... which is why I need someone to take a look at it for me. My php is very basic knowledge and I just got into it last week so I'm kinda just in the trial by error stage which isn't really what I need right now since The people I'm doing this for want the website up ASAP.

  4. #4
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: Some PHP Help

    To be honest I have never worked with cookies so I can only give you an overview, but your main page (or rather your members.php include) could look like this:
    Code:
    <?
    if (logged in cookie is set) {
    // echo the log out link
    }
    else {
    // echo the login form
    }
    ?>
    This way, every time your page will load, it will check if the user is logged in or not and show the login or log out accordingly. All you have to do is change your IF clause to check your cookie and place your content (login/log out) in the proper clauses.
    Last edited by Salvatos; 09-18-2008 at 12:38 PM.

  5. #5
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Some PHP Help

    OK, Logout needs some additional features (in addition to the cookie clear out above) to completely clear the session.

    Very simply, just add the following code to a seperate page and amend the variables to suit your site.

    PHP Code:
    // *** Logout the current user.

    // specify the redirect after logout
    $logoutGoTo "../index.php";
    // if there is no session, start session
    if (!isset($_SESSION)) {
      
    session_start();
    }
    //Specify all session variables and null them out

      
    $_SESSION['Username'] = NULL;
      
    $_SESSION['WorkgroupID'] = NULL;
      
    $_SESSION['AccessLevel'] = NULL;
      
    $_SESSION['Usertimezone'] = NULL;
      
    $_SESSION['Usertimeformat'] = NULL;

    //unset all session variables
      
    unset($_SESSION['Username']);
      unset(
    $_SESSION['WorkgroupID']);
      unset(
    $_SESSION['AccessLevel']);
      unset(
    $_SESSION['Usertimezone']);
      unset(
    $_SESSION['Usertimeformat']);

    //use the redirect variable to forward to another specified page after logout.

    if ($logoutGoTo != "") {header("Location: $logoutGoTo");
    exit;

    I use this on my site - check it out.

    As for your other issue, I'm not sure if you want this "title" to alter depending on who is logged in. If this is what you want, simply add another field to the users record [TITLE, varchar (30)]

    The admin entry form (which I'm assuming you can create) would then simply update that record with the new title.

    To show it on your index page, there are two mthods I can think of.

    1) set the [TITLE] from the database as a session variable like $_SESSION['var_page_title'] and then on each page you want to show it, simply use echo.

    PHP Code:
    <?php echo $_SESSION['var_page_title'];?>
    The other solution is a bit more complex and involves creating a recordset on each page that automatically loads from the DB.

    You can then echo as before

    PHP Code:
    <?php echo $row_RecordsetOfTitle['TITLE'];?>
    ...I prefer the session route and again, I use this method for users with time or timeformat preferences.

  6. #6
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: Some PHP Help

    I just want to say that this part is useless:
    PHP Code:
    if ($logoutGoTo != "") {header("Location: $logoutGoTo"); 
    considering you set the variable and never change it.

    You should just change it to
    PHP Code:
    header("Location: http://MYSITE.com/index.php"); 

  7. #7
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Some PHP Help

    Quote Originally Posted by VPmase View Post
    I just want to say that this part is useless:
    PHP Code:
    if ($logoutGoTo != "") {header("Location: $logoutGoTo"); 
    considering you set the variable and never change it.

    You should just change it to
    PHP Code:
    header("Location: [/quote'>http://MYSITE.com/index.php"); 
    Good point!!!

    This is what happens when you butcher part of a larger script!

  8. #8
    Jober68 is offline x10Hosting Member Jober68 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    62

    Re: Some PHP Help

    so would any of you masters like to have a look at my script and fix it for me? to be honest what you guys are putting in above doesn't look at all like it would work with my login script

  9. #9
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Some PHP Help

    Quote Originally Posted by Jober68 View Post
    so would any of you masters like to have a look at my script and fix it for me? to be honest what you guys are putting in above doesn't look at all like it would work with my login script
    Unless you have a very strange login/validation script, the above tools should work fine and they are pretty standard.

    If you want any of us to have a go - try posting the login script and let us know all session variables you have running.

  10. #10
    Jober68 is offline x10Hosting Member Jober68 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    62

    Re: Some PHP Help

    the problem isn't with the login script it's my main page that I want to change depending on if someone is logged in or not but I shall post everything and maybe someone can edit my main page :P

    This is what is on my index page that goes to my dynamic page that contains my login information:
    Code:
      <div id="left">
    <?php
    require_once "./lmenu2.php";
    ?>

    This is my lmenu2.php. This is what get's displayed on my main page and is where I think my error is. I want it so that if I am logged in it shows members.php and if I'm not logged in it shows the login.php however it only ever shows members.php.
    Code:
    <?php
    	function checkLogin($levels)
    	{
    		if(!$_SESSION['logged_in'])
    		{
    			$access = FALSE;
    		}
    		else {
    			$kt = split(' ', $levels);
    			
    			$query = mysql_query('SELECT Level_access FROM users WHERE ID = "'.mysql_real_escape_string($_SESSION['user_id']).'"');
    			$row = mysql_fetch_assoc($query);
    			
    			$access = FALSE;
    			
    			while(list($key,$val)=each($kt))
    			{
    				if($val==$row['Level_access'])
    				{//if the user level matches one of the allowed levels
    					$access = TRUE;
    				}
    			}
    		}
    		if($access==FALSE)
    		{
    			include_once("login/members.php");
    
    		}
    		else {
    			include_once("login/login.php");
    		}
    		if($access==TRUE)
    		{
    			include_once("login/login.php");			
    		}
    		
    	}
    checkLogin('1 2');
    	?>

    Here is my login code:
    Code:
    require_once('db.php');
    include('functions.php');
    
    	if(isset($_POST['Login']))
    	{
    		if($_POST['username']!='' && $_POST['password']!='')
    		{
    			//Use the input username and password and check against 'users' table
    			$query = mysql_query('SELECT ID, Username, Active FROM users WHERE Username = "'.mysql_real_escape_string($_POST['username']).'" AND Password = "'.mysql_real_escape_string(md5($_POST['password'])).'"');
    			
    			if(mysql_num_rows($query) == 1)
    			{
    				$row = mysql_fetch_assoc($query);
    				if($row['Active'] == 1)
    				{
    					session_start();
    					$_SESSION['user_id'] = $row['ID'];
    					$_SESSION['logged_in'] = TRUE;
    					header("Location: http://www.christian-debate.com/barbershop/index.php");
    				}
    				else {
    					$error = 'Your membership was not activated. Please open the email that we sent and click on the activation link';
    				}
    			}
    			else {		
    				$error = 'Login failed !';		
    			}
    		}
    		else {
    			$error = 'Please user both your username and password to access your account';
    		}
    	}
    And my members.php code: (btw can anyone write up the logout code for this using some type of submit form?
    Code:
    <?php 
    session_start(); 
    require_once('db.php'); 
    include('functions.php'); 
    
    $query = "SELECT * FROM users"; 
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result) or die(mysql_error());
    
    echo "Welcome <b>" .$row['Username']."</b>. You are now logged in.";
    ?>
    <form action="login/logout.php" >
    	<input type="submit" value="Logout" action="$logout"/><br/>
    </form>

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers