Jump to content

Recommended Posts

So others do not have to download it.

 

connect.php

 

<?php
    define('DB_HOST','localhost');
    define('DB_USER','ben');
    define('DB_PASS','');
    define('DB_NAME','tightcms.com');
    
    $connection = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());
?>

 

functions.php

 

<?php
    include "connect.php";
    
    $site = new site;
    $admin = new admin;
    
    class site {
        function name() {
            $query = mysql_query("SELECT * FROM site_config") or die(mysql_error());
            while($sn = mysql_fetch_assoc($query)) {
                echo "<h1>" . $sn['sitename'] . "</h1>";
            }
        }
        
        function login($username, $password) {
                if(isset($username)) {
                    if(isset($password)) {
                        include "connect.php";
                        $query = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
                        $user = mysql_fetch_array($query);
                        
                        if(md5($password) == $user['password']) {
                        $_SESSION['user'] = $user['username'];
                        $_SESSION['userid'] = $user['id'];
                        echo '<script>alert("Successfully Logged In!");</script>';
                        header("Location: index.php");
                    } else {
                        echo "<script>alert('Please check your login details!');</script>";
                        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';    
                        exit;
                    }
                } else {
                  echo "Please check your password!";
                  include "login.php";
                  exit;
                }
            } else {
                echo "Please check your username!";
                include "login.php";
                exit;
            }
        }
    }
    
    class admin {
        function navigation() {
            echo "<ul>";
            echo "<li><a href='index.php'>Home</a></li>";
            echo "<li><a href='logout.php'>Logout</a></li>";
            echo "</ul>";
        }
    }
?>

 

index.php

 

<?php
    include "functions.php";
    session_start();  
    if(isset($_SESSION['user'])) {
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<?php
    } else {
        header("Location:  login.php");
    }
?>

 

login.php

 

<!DOCTYPE HTML>
<html>
    <head>
        <?php
            include "functions.php";
            if (!empty($_POST['login-submit'])) {
                $site->login($_POST['username'], $_POST['password']);
            }
        ?>
    </head>
    <body>
        <?php
            session_start();
            if(isset($_SESSION['user'])) {
                header("Location: index.php");
            } else {
        ?>
            <form name="login" method="post">
                <table>
                    <tr>
                        <td><label for="username" id="white" align="center">Username: </label></td>
                        <td><input type="text" name="username" /></td>
                    </tr>
                    <tr>
                        <td><label for="password" id="white">Password: </label></td>
                        <td><input type="password" name="password" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" name="login-submit" value="Login!" /></td>
                    </tr>
                </table>
            </form>
        <?php
            }
        ?>
    </body>
</html>

 

logout.php

 

<?php
    session_start();
    session_destroy();
    header("Location: login.php");
?>

 

users.sql

 

-- phpMyAdmin SQL Dump
-- version 3.5.6
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 20, 2013 at 08:13 PM
-- Server version: 5.5.29-log
-- PHP Version: 5.3.21

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `tightcms.com`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `full_name` mediumtext NOT NULL,
  `username` varchar(250) NOT NULL,
  `password` varchar(150) NOT NULL,
  `date_of_birth` varchar( NOT NULL,
  `bio` longtext NOT NULL,
  `eal` varchar(1) DEFAULT NULL,
  `pic_path` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `full_name`, `username`, `password`, `date_of_birth`, `bio`, `eal`, `pic_path`) VALUES
(1, 'Administrator', 'admin', '9cdfb439c7876e703e307864c9167a15', '18/05/00', 'lol lol lol', '1', NULL);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
 
Link to comment
https://forums.phpfreaks.com/topic/281404-session-help/#findComment-1446006
Share on other sites

I've only had a quick glance but one thing that sticks out is on login.php. What's happening is you're not starting the session before you set session data, therefore, it's not getting stored. You're then redirecting the user within your class (site::login()) to index.php when session_start is invoked and obviously the check fails.

 

So the first fix would be to move session_start to the very TOP of every page. There may be other issues but that's the first one you need to resolve.

 

Nice to see people having a go at OOP by the way! Good job.

 

Any problems then post back :)

Link to comment
https://forums.phpfreaks.com/topic/281404-session-help/#findComment-1446135
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.