JTightStuffB Posted August 20, 2013 Share Posted August 20, 2013 Hi There, I am trying to make a login system with PHP and I have made one but I can't see the problem, it just keeps taking me back to login.php. All files involved attached. Many Thanks for Your Help in Advance. includes.zip Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted August 20, 2013 Share Posted August 20, 2013 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 */; Quote Link to comment Share on other sites More sharing options...
exeTrix Posted August 21, 2013 Share Posted August 21, 2013 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.