Jump to content

Help with a news script.


Flames

Recommended Posts

Its not the actual how to do it that i cant get working, but for some reason the script won't run, is it just too long? i checked and double-checked everything all the {} match (even commented count!), no whitespaces in unwanted places etc. But no matter what the variable is the page just wont load anything. The source code just shows blank, and it isnt the minor amount of JS in there because FF error console doesnt give me errors.

 

<?php
require_once("connect.php");
mysql_select_db($db['db'], $con) or die("There was an error connecting to the database.");
$login = $_POST['login'];
$send = $_POST['send'];
$user = $_POST['username'];
$user = stripslashes($user);
$user = mysql_real_escape_string($user);
$pass = $_POST['password'];
$pass = stripslashes($pass);
$pass = mysql_real_escape_string($pass);
$pass2 = md5($pass);
$message = $_POST['message1'];
$message2 = $_POST['message2'];
$poster = $user;

function checkLogin($c_user, $c_pass)
{ //1
$c_user = stripslashes($c_user);
$c_user = mysql_real_escape_string($c_user);
$c_pass = stripslashes($c_pass);
$c_pass = mysql_real_escape_string($c_pass);
$sql = "SELECT * FROM Account WHERE username = '$c_user' AND password = '$c_pass'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count==1){ //2
session_register("user");
session_register("pass");
global $online;
$online = "1";
} //1
else
{ //2
global $online;
$online = "0";
} //1
if($_GET['error'] == "1")
{ //2
$error = "Wrong Username or Password. Please try again below.";
} //1

if($login == "yes")
{ //2
$sql = "SELECT * FROM User WHERE username = '$user' AND password = '$pass2'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count==1){ //3
setcookie("user", $user);
setcookie("pass", $pass2);
header("location:add_news.php");
} //2
else
{ //3
header("location:add_news.php?error=1");
} //2



} //1
elseif(isset($_COOKIE['user']) && isset($_COOKIE['pass']))
{ //2
checkLogin($_COOKIE['user'], $_COOKIE['pass']);
if($online == 0)
{ //3

setcookie("user", $user, "", -600);
setcookie("pass", $pass2, "", -600);
header("location:add_news.php");
} //2
elseif($online == 1)
{ //3
echo <<<EOT
<html>
<head>
<link rel="stylesheet" type="text/css"
href="style.css" />
<title>
eFlame News
</title>
<script type="text\javascript">
function MessageFunction()
var message
message = document.news_form.message1.value;
document.news_form.message2.value = message;
</script>
</head>
<body>

<div id="apDiv1">

<a href="index.php"><img src="link_imgs/home.png" alt="HOME" width="47" height="23" border="0" /></a>
<a href="help.php"><img src="link_imgs/help.png" alt="HELP" width="47" height="23" border="0" /></a>
<a href="about.php"><img src="link_imgs/about.png" alt="ABOUT" width="47" height="23" border="0" /></a>
<a href="forum/"><img src="link_imgs/forum.png" alt="FORUM" width="47" height="23" border="0" /></a>
<a href="news.php"><img src="link_imgs/news.png" alt="NEWS" width="47" height="23" border="0" /></a></div>

<div align="right" id="apDiv2">
<a href="login.php"><img src="link_imgs/login.png" alt="LOGIN" width="47" height="23" align="left" border="0" /></a>
<a href="register.php"><img src="link_imgs/register.png" alt="REGISTER" width="60" height="23" border="0" /></a></div>

<div class="style1" id="bgplacer">
<div id = "Page">
<form action="add_news.php" method ="post" id="news_form">
Small News: <textarea rows="5" cols="50" wrap="physical" name="message1" onKeyUp='MessageFunction()' /></textarea><br />
Full news: <textarea rows="20" cols="50" wrap="physical" name="message2" /></textarea><br />
<input type="hidden" name="send" value="yes" />
<input type="submit" value="News it." />
</form>
</div>
</div>
</body>
</html>
EOT;
} //2
else
{ //3
header("location:add_news.php");
} //2
} //1
elseif($send == "yes")
{ //2
$sql = "INSERT INTO News(message, message2, poster) VALUES($message, $message2, $poster)";
mysql_query($sql);
} //1
else
{ //2
echo <<<EOT
<html>
<head>
<link rel="stylesheet" type="text/css"
href="style.css" />
<title>
eFlame News
</title>
</head>
<body>

<div id="apDiv1">

<a href="index.php"><img src="link_imgs/home.png" alt="HOME" width="47" height="23" border="0" /></a>
<a href="help.php"><img src="link_imgs/help.png" alt="HELP" width="47" height="23" border="0" /></a>
<a href="about.php"><img src="link_imgs/about.png" alt="ABOUT" width="47" height="23" border="0" /></a>
<a href="forum/"><img src="link_imgs/forum.png" alt="FORUM" width="47" height="23" border="0" /></a>
<a href="news.php"><img src="link_imgs/news.png" alt="NEWS" width="47" height="23" border="0" /></a></div>

<div align="right" id="apDiv2">
<a href="login.php"><img src="link_imgs/login.png" alt="LOGIN" width="47" height="23" align="left" border="0" /></a>
<a href="register.php"><img src="link_imgs/register.png" alt="REGISTER" width="60" height="23" border="0" /></a></div>

<div class="style1" id="bgplacer">
<div id = "Page">
<form action="add_news.php" method ="post" id="login_form">
$error <br />
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="hidden" name="login" value="yes" />
<input type="submit" value="Login" />
</form>
</div>
</div>
</body>
</html>
EOT;
} //1
} //0

?>

Link to comment
Share on other sites

forgot to mention i've tried that and this is all i get. Notice: Undefined index: login in /home/a6474246/public_html/add_news.php on line 6

 

 

PHP Error Message

 

Notice: Undefined index: login in /home/a6474246/public_html/add_news.php on line 6

 

Free Web Hosting

 

PHP Error Message

 

Notice: Undefined index: send in /home/a6474246/public_html/add_news.php on line 7

 

Free Web Hosting

 

PHP Error Message

 

Notice: Undefined index: username in /home/a6474246/public_html/add_news.php on line 8

 

Free Web Hosting

 

PHP Error Message

 

Notice: Undefined index: password in /home/a6474246/public_html/add_news.php on line 11

 

Free Web Hosting

 

PHP Error Message

 

Notice: Undefined index: message1 in /home/a6474246/public_html/add_news.php on line 15

 

Free Web Hosting

 

PHP Error Message

 

Notice: Undefined index: message2 in /home/a6474246/public_html/add_news.php on line 16

 

Free Web Hosting

Link to comment
Share on other sites

Notices wont prevent your script from running.

 

Is that all the code?

 

If it is it'll never output anything until you call the checkLogin() function (I cannot see you currently doing this).

 

Also indenting your code can make it more readable/easier to identify missing {} braces.

Link to comment
Share on other sites

Just had a thought, should i split it up into a login.php and an add_news.php page. I was going to do this but i didnt want to because i was unsure about how i could make it 100% secure, so i thought i would have one page which was secure. i have read the tutorial on security and sessions and cookies are easy to hack so what do i do?

Link to comment
Share on other sites

i have used the checkLogin function here

elseif(isset($_COOKIE['user']) && isset($_COOKIE['pass']))
{ //2
checkLogin($_COOKIE['user'], $_COOKIE['pass']);
if($online == 0)
{ //3

its definately not {} because each one has been commented.

Thats defined within your function. You still need to call the function to initialise the code within it. PHP wont automatically call the function for you.

Link to comment
Share on other sites

I went to the trouble of indenting your code so that YOU can see what it is logically doing -

 

<?php
require_once("connect.php");
mysql_select_db($db['db'], $con) or die("There was an error connecting to the database.");
$login = $_POST['login'];
$send = $_POST['send'];
$user = $_POST['username'];
$user = stripslashes($user);
$user = mysql_real_escape_string($user);
$pass = $_POST['password'];
$pass = stripslashes($pass);
$pass = mysql_real_escape_string($pass);
$pass2 = md5($pass);
$message = $_POST['message1'];
$message2 = $_POST['message2'];
$poster = $user;

function checkLogin($c_user, $c_pass)
{ //1
$c_user = stripslashes($c_user);
$c_user = mysql_real_escape_string($c_user);
$c_pass = stripslashes($c_pass);
$c_pass = mysql_real_escape_string($c_pass);
$sql = "SELECT * FROM Account WHERE username = '$c_user' AND password = '$c_pass'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count==1){ //2
	session_register("user");
	session_register("pass");
	global $online;
	$online = "1";
} //1
else
{ //2
	global $online;
	$online = "0";
} //1
if($_GET['error'] == "1")
{ //2
	$error = "Wrong Username or Password. Please try again below.";
} //1

if($login == "yes")
{ //2
	$sql = "SELECT * FROM User WHERE username = '$user' AND password = '$pass2'";
	$query = mysql_query($sql);
	$count = mysql_num_rows($query);
	if($count==1){ //3
		setcookie("user", $user);
		setcookie("pass", $pass2);
		header("location:add_news.php");
	} //2
	else
	{ //3
		header("location:add_news.php?error=1");
	} //2
} //1
elseif(isset($_COOKIE['user']) && isset($_COOKIE['pass']))
{ //2
	checkLogin($_COOKIE['user'], $_COOKIE['pass']);
	if($online == 0)
	{ //3
		setcookie("user", $user, "", -600);
		setcookie("pass", $pass2, "", -600);
		header("location:add_news.php");
	} //2
	elseif($online == 1)
	{ //3
		echo <<<EOT
		<html>
		<head>
		<link rel="stylesheet" type="text/css"
		href="style.css" />
		<title>
		eFlame News
		</title>
		<script type="text\javascript">
		function MessageFunction()
		var message
		message = document.news_form.message1.value;
		document.news_form.message2.value = message;
		</script>
		</head>
		<body>
		<div id="apDiv1">
		<a href="index.php"><img src="link_imgs/home.png" alt="HOME" width="47" height="23" border="0" /></a>
		<a href="help.php"><img src="link_imgs/help.png" alt="HELP" width="47" height="23" border="0" /></a>
		<a href="about.php"><img src="link_imgs/about.png" alt="ABOUT" width="47" height="23" border="0" /></a>
		<a href="forum/"><img src="link_imgs/forum.png" alt="FORUM" width="47" height="23" border="0" /></a>
		<a href="news.php"><img src="link_imgs/news.png" alt="NEWS" width="47" height="23" border="0" /></a></div>
		<div align="right" id="apDiv2">
		<a href="login.php"><img src="link_imgs/login.png" alt="LOGIN" width="47" height="23" align="left" border="0" /></a>
		<a href="register.php"><img src="link_imgs/register.png" alt="REGISTER" width="60" height="23" border="0" /></a></div>
		<div class="style1" id="bgplacer">
		<div id = "Page">
		<form action="add_news.php" method ="post" id="news_form">
		Small News: <textarea rows="5" cols="50" wrap="physical" name="message1" onKeyUp='MessageFunction()' /></textarea><br />
		Full news: <textarea rows="20" cols="50" wrap="physical" name="message2" /></textarea><br />
		<input type="hidden" name="send" value="yes" />
		<input type="submit" value="News it." />
		</form>
		</div>
		</div>
		</body>
		</html>
EOT;
	} //2
	else
	{ //3
		header("location:add_news.php");
	} //2
} //1
elseif($send == "yes")
{ //2
	$sql = "INSERT INTO News(message, message2, poster) VALUES($message, $message2, $poster)";
	mysql_query($sql);
} //1
else
{ //2
	echo <<<EOT
	<html>
	<head>
	<link rel="stylesheet" type="text/css"
	href="style.css" />
	<title>
	eFlame News
	</title>
	</head>
	<body>
	<div id="apDiv1">
	<a href="index.php"><img src="link_imgs/home.png" alt="HOME" width="47" height="23" border="0" /></a>
	<a href="help.php"><img src="link_imgs/help.png" alt="HELP" width="47" height="23" border="0" /></a>
	<a href="about.php"><img src="link_imgs/about.png" alt="ABOUT" width="47" height="23" border="0" /></a>
	<a href="forum/"><img src="link_imgs/forum.png" alt="FORUM" width="47" height="23" border="0" /></a>
	<a href="news.php"><img src="link_imgs/news.png" alt="NEWS" width="47" height="23" border="0" /></a></div>
	<div align="right" id="apDiv2">
	<a href="login.php"><img src="link_imgs/login.png" alt="LOGIN" width="47" height="23" align="left" border="0" /></a>
	<a href="register.php"><img src="link_imgs/register.png" alt="REGISTER" width="60" height="23" border="0" /></a></div>
	<div class="style1" id="bgplacer">
	<div id = "Page">
	<form action="add_news.php" method ="post" id="login_form">
	$error <br />
	Username: <input type="text" name="username" /><br />
	Password: <input type="password" name="password" /><br />
	<input type="hidden" name="login" value="yes" />
	<input type="submit" value="Login" />
	</form>
	</div>
	</div>
	</body>
	</html>
EOT;
} //1
} //0
?>

 

The closing } for your function definition is at the end of the code. I suspect that you intended it to be at about line 41 in the posted code.

 

Your code has two other problems -

 

1) The purpose of a function is to return a value so that you can replace a block of code with a function call and the value the code would have given you is returned by the function call instead.

 

Don't use global $online; in your function. Write the code in the function to return the value that you want to assign to $online and then where you call the function assign the return value to $online -

 

$online = checkLogin($_COOKIE['user'], $_COOKIE['pass']);

 

2) You are also using session_register(). This function was turned off by default in php4.2 in the year 2002 and has been completely removed in php6. You need to use the corresponding $_SESSION variables (and put a session_start(); at the beginning of your code.)

Link to comment
Share on other sites

well it now shows code everything work except actually sending the news(and JS which ill fix later), it just shows the news page again and does nothing. Here is my code

<?php
//ini_set('display_errors', '1');
//error_reporting(E_ALL);
require_once("connect.php");
mysql_select_db($db['db'], $con) or die("There was an error connecting to the database.");
$login = $_GET['login'];
$send = $_GET['send'];
$user = $_POST['username'];
$user = stripslashes($user);
$user = mysql_real_escape_string($user);
$pass = $_POST['password'];
$pass = stripslashes($pass);
$pass = mysql_real_escape_string($pass);
$pass2 = md5($pass);
$message = $_POST['message1'];
$message2 = $_POST['message2'];
$poster = $user;

function checkLogin($c_user, $c_pass)
{ //1
$c_user = stripslashes($c_user);
$c_user = mysql_real_escape_string($c_user);
$c_pass = stripslashes($c_pass);
$c_pass = mysql_real_escape_string($c_pass);
$sql = "SELECT * FROM Account WHERE username = '$c_user' AND password = '$c_pass'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count==1){ //2
session_register("user");
session_register("pass");
global $online;
$online = "1";
} //1
else
{ //2
global $online;
$online = "0";
} //1
} // 0
if($_GET['error'] == "1")
{ //1
$error = "Wrong Username or Password. Please try again below.";
} //0

if($login == "yes")
{ //1
$sql = "SELECT * FROM User WHERE username = '$user' AND password = '$pass2'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count==1){ //2
setcookie("user", $user);
setcookie("pass", $pass2);
header("location:add_news.php");
} //1
else
{ //2
header("location:add_news.php?error=1");
} //1



} //0
elseif(isset($_COOKIE['user']) && isset($_COOKIE['pass']))
{ //1
checkLogin($_COOKIE['user'], $_COOKIE['pass']);
if($online == 0)
{ //2

setcookie("user", $user, "", -600);
setcookie("pass", $pass2, "", -600);
header("location:add_news.php");
} //1
elseif($online == 1)
{ //2
echo <<<EOT
<html>
<head>
<link rel="stylesheet" type="text/css"
href="style.css" />
<title>
eFlame News
</title>
<script type="text\javascript">
function MessageFunction(value)
{
var message
message = value.value;
document.news_form.message2.value = message;
}
 </script>
</head>
<body>

<div id="apDiv1">

<a href="index.php"><img src="link_imgs/home.png" alt="HOME" width="47" height="23" border="0" /></a>
<a href="help.php"><img src="link_imgs/help.png" alt="HELP" width="47" height="23" border="0" /></a>
<a href="about.php"><img src="link_imgs/about.png" alt="ABOUT" width="47" height="23" border="0" /></a>
<a href="forum/"><img src="link_imgs/forum.png" alt="FORUM" width="47" height="23" border="0" /></a>
<a href="news.php"><img src="link_imgs/news.png" alt="NEWS" width="47" height="23" border="0" /></a></div>

<div align="right" id="apDiv2">
<a href="login.php"><img src="link_imgs/login.png" alt="LOGIN" width="47" height="23" align="left" border="0" /></a>
<a href="register.php"><img src="link_imgs/register.png" alt="REGISTER" width="60" height="23" border="0" /></a></div>

<div class="style1" id="bgplacer">
<div id = "Page">
<form action="add_news.php?send=yes" method ="post" id="news_form">
Small News: <textarea rows="5" cols="50" wrap="physical" name="message1" onKeyUp='MessageFunction(this)' /></textarea><br />
Full news: <textarea rows="20" cols="50" wrap="physical" name="message2" /></textarea><br />
<input type="submit" value="News it." name="news" />
</form>
</div>
</div>
</body>
</html>
EOT;
} //1
else
{ //2
header("location:add_news.php");
} //1
} //0
elseif($send == "yes" )
{ //1
$sql = "INSERT INTO News(message, message2, poster) VALUES($message, $message2, $poster)";
mysql_query($sql);
echo "News succussfully added";
} //0
else
{ //1
echo <<<EOT
<html>
<head>
<link rel="stylesheet" type="text/css"
href="style.css" />
<title>
eFlame News
</title>
</head>
<body>

<div id="apDiv1">

<a href="index.php"><img src="link_imgs/home.png" alt="HOME" width="47" height="23" border="0" /></a>
<a href="help.php"><img src="link_imgs/help.png" alt="HELP" width="47" height="23" border="0" /></a>
<a href="about.php"><img src="link_imgs/about.png" alt="ABOUT" width="47" height="23" border="0" /></a>
<a href="forum/"><img src="link_imgs/forum.png" alt="FORUM" width="47" height="23" border="0" /></a>
<a href="news.php"><img src="link_imgs/news.png" alt="NEWS" width="47" height="23" border="0" /></a></div>

<div align="right" id="apDiv2">
<a href="login.php"><img src="link_imgs/login.png" alt="LOGIN" width="47" height="23" align="left" border="0" /></a>
<a href="register.php"><img src="link_imgs/register.png" alt="REGISTER" width="60" height="23" border="0" /></a></div>

<div class="style1" id="bgplacer">
<div id = "Page">
<form action="add_news.php?login=yes" method ="post" id="login_form">
$error <br />
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="Login" name="login" />
</form>
</div>
</div>
</body>
</html>
EOT;
} //0

?>

as for not indenting i dont have any programs to use to write code in other than notepad and my webhosts code editor which cant use tabs, if you press it it changes to the url bar instead.

Link to comment
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.