Page 1 of 1

PHP Help Please

Posted: Thu May 07, 2015 2:05 pm
by Mark
Im having a problem once I login to my account its ment to redirect to the index page once logged in and when I redirect to the index page the links for login and register are ment to not display and its ment to show logged in. Can anyone help me please?

login page code.
Code: Select all
<?php include('styles/top.php'); ?> 
 
      <?php 
        include 'connect.php'; 
        include 'functions.php'; 
      ?> 
       
    <div id="left"> 

    <form method="post"> 
     
    <?php 
         
        if(isset($_POST['login_button'])){ 
            $username = $_POST['username'];  
            $password = $_POST['password'];  
             
            if(empty($username) or empty($password)){ 
               echo "The username or Password field was empty please try again!.";  
            } else { 
                $query = mysql_query("SELECT id FROM users WHERE username='$username' AND password='$password'"); 
                $run = mysql_fetch_array($query); 
                $id = $run['id']; 
                 
                if (!empty($id)){ 
                 
                $_SESSION['user_id'] = $id; 
                 
                echo "<meta http-equiv='refresh' content='0;url=index.php'>"; 
             
                } else { 
                    echo "Wrong Username or Password was entered."; 
                } 
            } 
              
        } 
     
    ?> 
     
    <table cellspacing="20"> 
    <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><input type="submit" name="login_button" value="Login" /></td> 
    </tr> 
    </table> 

    </form> 

    </div> 
     
    <div id="right"> 
     
     
    </div> 


<?php include('styles/bottom.php'); ?> 
functions.php code
Code: Select all
<?php 

session_start(); 

    function loggedin(){ 
         
        if(isset($_SESSION['user_id'])&&!empty($_SESSION['user_id'])){ 
            return true; 
        } else { 
            return false;     
        } 
         
    } 

?> 
index.php code
Code: Select all
<?php include('styles/top.php'); ?> 
 
      <?php 
            include 'connect.php'; 
            include 'functions.php'; 
        ?> 
 
    <div id="left"> 

    </div> 
     
    <div id="right"> 

     
        <?php 
            if(loggedin()){ 
                echo "Logged in ! <a href=logout.php>Logout</a>"; 
            } else { 
        ?> 
        <a href="login.php">Login</a> | <a href="register.php">Register</a> 
        <?php 
        } 
        ?> 
    </div> 


<?php include('styles/bottom.php'); ?> 

Re: PHP Help Please

Posted: Thu May 07, 2015 3:16 pm
by Codex
How is the login session stored? Are you using cookies to save the session?
I might be able to help you out, otherwise Filip is better than me

Re: PHP Help Please

Posted: Thu May 07, 2015 3:24 pm
by Mark
Hello,

Here is the login script but now that i have it to login i cant get it to destroy the session lol if i logout its ment to show the login and register links and not the loggedin here is the login page code for the session.

if you got to http://www.mbappz.co.uk/login.php and use username: test and Password: test

and click logout you will see what i mean?
Code: Select all
   <form method='post'>
    <?php
    
        if(isset($_POST['login_button'])){
           $username = $_POST['username'];
           $password = $_POST['password'];
           
           if(empty($username) or empty($password)){
                echo 'Username or Password is empty, Please re-check the form!.';
           } else {
                $query = mysql_query("SELECT id FROM users WHERE username='$username' AND password='$password'");
                $run = mysql_fetch_array($query);
                $id = $run['id'];
                
                if(!empty($id)){
                
                $_SESSION['user_id'] = $id;
                
                header('location: index.php');
           } else {
                echo 'Invalid Username or Password, Please try again!.';
           }
        }
           
    }   
         
    ?>
    
    <table>
    <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></td>
        <td><input type='submit' name='login_button' value='Login' /></td>
    </tr>
    </table>
    </form>
index page
Code: Select all
<?php include('styles/top.php'); ?>



    <div id="left">

    </div>
    
    <div id="right">

        <p>Welcome</p>
        
        <?php
            if(loggedin){
                echo 'Logged In ! <a href=logout.php>Logout</a>';
            } else {
        ?>
        
       <a href="login.php">Login</a>
       <a href="register.php">Register</a>
        
        <?php
            }
        ?>
    </div>


<?php include('styles/bottom.php'); ?>
logout.php
Code: Select all
<?php

include 'connect.php';
include 'functions.php';

session_destroy();

header('location: index.php');

?>

Re: PHP Help Please

Posted: Thu May 07, 2015 3:40 pm
by Mark
I have attached the source code i cant get it to logout and when i click logout its ment to redifrect back to index.php and show login | register and if logged in it shows logged in!?

this peace of code on index.php
Code: Select all
    <div id="right">

        <p>Welcome</p>
        
        <?php
            if(loggedin){
                echo 'Logged In ! <a href="logout.php">Logout</a>';
            } else {
        ?>
        <table>
        <tr>
            <td><a href="login.php">Login</a></td>
        </tr>
         <tr>
            <td><a href="register.php">Register</a></td>
        </tr>
        </table>
        
        <?php
            }
        ?>
    </div>
its ment to show the table code if im not logged in but if i am logged in i should say Logged In with logout link but when i click logout it still shows the echo 'Logged In ! <a href="logout.php">Logout</a>'; what have i done wrong?.

Re: PHP Help Please

Posted: Fri May 08, 2015 11:15 pm
by Codex
Hey again,

I won't be able to help you much at the moment due to busy schedule - exams, but I will be done 21st/22nd (so don't think I'm ignoring you)

- Codex