Jump to content


news/user integration


3 replies to this topic

#1 Aussiestinger

    Young Padawan

  • Members
  • Pip
  • 270 posts

Posted 03 May 2006 - 05:18 PM

I took the tutorial for the news CMS from greycobra, only done part one so far, but it works fine (part 2 is edit/delete), just I am trying to integrate a User System into it (which I already have set up) anyways here's the submit.php

 <?php
include("./dbconnect.php");//include the file that connects to the database
if(!empty($title)) {//if the title is not empty, than do this function
$title = addslashes($title);
$user = addslashes($user);
$message = addslashes($message);

$date = date("F j, Y");//set up the date format
$date2 = mktime();

$sql = "INSERT INTO mynews (id, title, user, message, date) VALUES ('NULL', '$title','$user','$message','$date')";//Insert all of your information into the mynews table in your database
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
echo "Database Updated.";
} else {//However, if the title variable is empty, than do this function
?>
<form name="news" method="post" action="<?php echo $PHP_SELF; ?>">
<p>Please fill out all of the following fields:</p>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="117"><font size="1">News Topic/Title*: </font> </td>
<td width="577">
<font size="1">
<input type="text" name="title" size="50">
</font>
</td>
</tr>
<tr>
<td width="117"><font size="1">Username*:</font></td>
<td width="577">
<font size="1">
<input type="text" name="user" size="50">
</font>
</td>
</tr>
<tr>
<td width="117"><font size="1">Message*:</font></td>
<td width="577">
<font size="1">
<textarea name="message" rows=10 cols=43></textarea>
</font>
</td>
</tr>
</table>
<p>
<font size="1">
<input type="submit" name="Submit" value="Submit"></font>
</p>
</form> <?php
}//end this function
?>

and this is the code so only registered users can see a page;

<?PHP
include("include/session.php");
if($session->logged_in){
echo "what you see as registered";
}
else{
echo "what you see as guest.";
}

?>

So I would like to have it so you have to be registered to be able to see the submit.php page, anyone know how i would do this? also I would like it so only admins can see it, anyone know how to do that? the admins level is 9 (both news and user groups is using the same database) and user 1, guest 0.

Hope someone can help, thanks. :XD:

#2 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 03 May 2006 - 09:02 PM

Do you know how to use OOP? I am assuming you do because you have it in your last sample. Try using this idea of a class below:

I havn't tested it, but it should work. Just intergrate it with your user database and you should be good!

<?php

class session
{
	var $isAdmin;
	var $isGuest;
	var $isUser;
	var $userInfo;
	
	function authorize()
	{
		$this->resetUser();
		
		$fetch = mysql_query("SELECT * FROM users WHERE userID = '".$_COOKIE['userID']."' AND userHash = '".$_COOKIE['userHash']."' LIMIT 1") or die(mysql_error());
		
		if ( mysql_num_rows($fetch) == 1 )
		{
			$this->userInfo = mysql_fetch_array($fetch);
		}
		
		$this->setUser();
	}
	
	function setUser()
	{
		if ( isset($this->userInfo) && is_array($this->userInfo) )
		{
			if ( is_numeric($this->userInfo['userID']) )
			{
				$this->isUser = TRUE;
				
				switch ( $this->userInfo['userLevel'] )
				{
					case 'Administrator':
						$this->isAdmin = TRUE;
						$this->isUser = TRUE;
					break;
					
					case 'User':
						$this->isAdmin = FALSE;
						$this->isUser = TRUE;
					break;
					
					default:
						$this->resetUser();
					break;
				}				
			}
			else
			{
				$this->resetUser();
			}
		}
		else
		{
			$this->resetUser();
		}
	}
	
	function resetUser()
	{
		$this->isAdmin		=	FALSE;
		$this->isUser		=	FALSE;
		$this->isGuest		=	FALSE;
		$this->userInfo		=	array();
	}
	
}

?>

Edited by Chaos King, 03 May 2006 - 09:03 PM.


#3 Aussiestinger

    Young Padawan

  • Members
  • Pip
  • 270 posts

Posted 03 May 2006 - 10:04 PM

I am a complete n00b with PHP, this is the first time I am trying to actually learn it, it was two tutorials and they both work just trying to integrate them, the user one works on other pages just don't know how to get it into this, but thanks I will take a read over your code.

#4 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 04 May 2006 - 04:50 AM

Sure thing. If you need help, just buzz me on IM. I'll be glad to be of help.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users