• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2025 Replies
  • 331 Replies
    1928 Replies

PHP Help Please

User avatar
Mark
VIP - Donator
Posts: 370

PHP Help Please

Thu May 07, 2015 3:05 pm

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

User avatar
Codex
Coding God
Posts: 2025

Re: PHP Help Please

Thu May 07, 2015 4:16 pm

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
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender

User avatar
Mark
VIP - Donator
Posts: 370

Re: PHP Help Please

Thu May 07, 2015 4:24 pm

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');

?>

User avatar
Mark
VIP - Donator
Posts: 370

Re: PHP Help Please

Thu May 07, 2015 4:40 pm

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?.
You do not have the required permissions to view the files attached to this post.

User avatar
Codex
Coding God
Posts: 2025

Re: PHP Help Please

Sat May 09, 2015 12:15 am

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
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender

Post Reply

Return to “Help & Support”