Beginner needs help getting a comment box to input data into database

My guess is that the user name that you typed in is not in your database.

To debug this, first check your database for the user name and make sure it is spelled correctly.
(Capitals do matter!)

Next, make sure your code is reading the database correctly.
In the dologin.php page, you can place some debug code to see the flow of the various IF clauses…

You can add this command:
die(“Got here…”); just about anywhere and see if it gets to that point in the code.
If it does, then, move the got-here down a few lines and try again. When you find the point that the code is broken, look at the lines above it and figure out what is wrong…

If you can’t figure it out, repost the new versions of the three pages so we can see the updated versions.

Good luck…

What’s the address for the tutorial?

Well, I gave you changes before that didn’t get made. I will give you them again. Here is the login.php page:

[php]

<?php session_start(): if(isset($_SESSION['user'])) header("Location: index.php"); ?> Basic CMS - Admin Area - Login
Username:
Password:
[/php]

dologin.php :
[php]

<?php include('includes/functions.php'); if(isset($_POST(['login'])){ if($_POST['username'])){ if($_POST['password'])){ $query = mysql_query("SELECT * users WHERE Username = mysql_real_escape_string ($_POST['username'])") or die(mysql_error()); $user = mysql_fetch_array($query); if(md5($_POST['password'] == $user['Password']){ echo "Login successful"; $_SESSION['user'] = $user['Username'}; header("Location: index.php"); }else{ echo "Please check your login details!"; include('login.php'); } }else{ echo "Please check your password is correct!"; include('login.php'); } }else{ echo "Please check your username is correct!"; include('login.php'); }else{ echo "Please check you filled out the login form!"; include('login.php'); } ?>

[/php]

index.php :
[php]

<?php session_start(): if(!isset($_SESSION['user'])) header("Location: login.php"); ?> Basic CMS - Admin Area Logged In! Welcome <?php> echo $_SESSION['user']; ?> [/php] So, try those and if any other errors, let us know!

Will do, thanks again!

I updated my code to the code you provided on all three pages but im still stuck on the dologin.php page.

I entered die(“Got here…”); in various places throughout the dologin.php page but on every attempt “Got here…” was not visable on the dologin.php page.

Just to double check I wasn’t making a mistake with my login details I created another user with new details and tried to login again with the same outcome.

Also, to make the login form visable I needed to have the PHP code within the body of the html document. In a previous comment ErnieAlex you said that headers would not work after html code has been on the page, could this be the issue?

Also, as you requested Richei, the link to the second part of the tutorial I have been following…

http://www.youtube.com/watch?v=u3ry84gg0fw

Part 2 [Super Improved] is where I have reached. Also I was not able to redirect my localhost:8888/cms/admin to the login.php using the first piece of PHP he used incase that was important.

Quick update, ive been attempting to add logout and editing functions to my admin area, index.php, and im getting nothing showing on screen. Not even a simple

This is test text

is working…

Well, again, it is hard to fix it without your code. Please show us, inside PHP tags, your current code and we will try to fix you up…

I am working here on and off today and tomorrow morning, so send the code and I will look at it fairly soon!

Sorry, I didn’t post the code because it matched your previous code.

Login.php
[php]

Basic CMS - Admin Area - Login <?PHP session_start(); if(isset($_SESSION['user'])) header("Location: index.php"); ?>
Username:
Password:
[/php]

Dologin.php
[php]

<?php include('includes/functions.php'); start_session(); if(isset($_POST(['login'])){ if($_POST['username'])){ if($_POST['password'])){ $query = mysql_query("SELECT * users WHERE Username = mysql_real_escape_string ($_POST['username'])") or die(mysql_error()); $user = mysql_fetch_array($query); if(md5($_POST['password'] == $user['Password']){ echo "Login successful"; $_SESSION['user'] = $user['Username'}; header("Location: admin/index.php"); }else{ echo "Please check your login details!"; include('login.php'); } }else{ echo "Please check your password is correct!"; include('login.php'); } }else{ echo "Please check your username is correct!"; include('login.php'); }else{ echo "Please check you filled out the login form!"; include('login.php'); } ?>[/php]

Index.php
[php]

<?php session_start(); if(!isset($_SESSION['user'])) header("Location: admin/login.php"); ?> Basic CMS - Admin Area

This is test text....it does not appear on screen.

Logged In! Welcome <?php> echo $_SESSION['user']; ?>

Logout
Manage Posts

[/php]

I attempted to debug with die(“Got here…”); but after inserting this code in every available space in my dologin.php page, it was never visable after logging in. It appears that after logging in I am redirected to the dologin.php page and nothing happens from there.

Once again, ill be very greatful for any help

I’ve managed to get a “Login succesful” message on the dologin.php page, code as follows:

[php]<?php
include(‘includes/functions.php’);
session_start();

if (isset($_POST[‘login’])) {
if(isset($_POST[‘username’])) {
if(isset($_POST[‘password’])) {

		$username = $_POST['username'];
		$query = mysql_query("SELECT * FROM users WHERE Username = '$username'") or die (mysql_error());
		$user = mysql_fetch_array($query);
		
		if(md5($_POST['password']) == $user['Password']) {
			echo "Login successful";
			$_SESSION['user'] = $user['Username'];
			header("Location: index.php");
		} else {
			
			echo "Please check your login details!";
			include('login.php');
		}			
	} else {
		echo "Please check your password!";
		include('login.php');
	}
} else {
	echo "Please check your username!";
	include('login.php');
}

} else {
echo “Please check that you filled out the login form!”;
include(‘login.php’);
}
?>[/php]

Just cant get it to redirect me to the index.php page. header(“Location: index.php”); isn’t working I assume?

Well, so, this is the code in question:
if(md5($_POST[‘password’]) == $user[‘Password’]) {
echo “Login successful”;
$_SESSION[‘user’] = $user[‘Username’];
header(“Location: index.php”);
} else {

Which we know works as you get the “Login successful”… So, if it gets to that line, the next line is executed and should work. The reasons this would not work usually is because the page does not exist. As you currently have it, header(“Location: index.php”); , it sends it to the index.php page which is in the same folder as the current page. Make sure that both of these files are in the same folder. What I am saying is that you are not using a folder in the redirect, so both the “dologin.php” file and the “index.php” file must both be in the same folder. If not, you can use a different header, like header(“Location: somefolder/index.php”); where the somefolder would be the name of the folder that index.php is in. If they are in the same folder, not sure.

Let us know if the files are inside the same folder… (PS: I think you are close!)

Im very much hoping im/we are close! Thanks for your patience.

The index.php file I am trying to reach is in the same folder as dologin.php. They are both within htdocs>cms>admin and I have tried:

header(“Location: cms/admin/index.php”);

header(“Location: admin/index.php”);

header(“Location: index.php”);

None of these took me to my index.php page. One thing that might be worth mentioning is I have another index.php page in the cms folder, before the admin folder. Thought id mention it just incase.

Well, if they are both in the same folder, use: header(“Location: index.php”);
What you need to try is to make sure the page is accessible…
So, first, type it in directly and make sure it loads.

Like type in www.mydomain.com/htdocs/cms/admin/index.php and see if it loads…

If it does, the only thing could be the line before the header line could be stopping it.
So, try to comment out the line: $_SESSION[‘user’] = $user[‘Username’];

Lastly, you do not have the first line in the PHP as “session_start();”… Perhaps that is not allowing the
session variables to be accessed. Not sure, but, move that to the first line and move the include to the second line. Really grasping here as it looks like it should work. (I have seen where session variables will sometimes not show up an error and cause code after it not to work.) You can tell your PHP code to display all errors this way: (Includes moving the session_start…)
[php]

<?php session_start(); error_reporting(E_ALL); ini_set('display_errors', '1'); include('includes/functions.php'); if (isset($_POST['login'])) { [/php] What this does is turn on all of the errors that are sometimes hidden by server defaults. It will show all errors and may show some new info that we can use to debug... Let us know...

Thank you again ErnieAlex but I reached a solution by removing the echo “Login succesful”;

This allowed me to connect to my index.php page but there’s something still not right about it.

index.php
[php]

Basic CMS - Admin Area <?php session_start(); if(isset($_SESSION['user'])) header("Location: admin/login.php"); ?>

Logged In! Welcome <?php> echo $_SESSION['user']; ?>

Logout
Manage Posts

This is some test text...

[/php]

Nothing displays on screen, not even the text in the

tags is visable?

Managed to sort it ErnieAlex, becoming a bit to reliant and jumped straight in with the quetions instead of trying to sort it myself.

Im hoping that will be all I’ll need for a while. Thanks again, you’ve been a huge help. I’m sure i’ll be reposting at some point with another problem, shall I carry it on in this thread?

Congrats on getting it solved. No, please start a new thread and don’t forget to post your new code inside PHP tags. (Helps us a lot!)

And, please read the tutorials that others have posted. Some are very handy explaining common routines.

CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service