Jump to content

[SOLVED] Problem: retrieving strings from a separate file.


kodie

Recommended Posts

Well i have my main page that includes a page depending on the url extension (index.php?id=PAGE). but the main page also includes a file containing mostly all the text strings being used in the sub pages.

 

If i haven't lost you yet, here's a breakdown of it all:

includes/strs.php (Contains all strings being used on other pages.)

pages/main.php (Page that is included by index.php)

pages/usercp/users.php (Page i'm having trouble with.)

index.php (Main page that includes above files. Calls pages by ?id=PAGE)

 

Ok, so here's an example of some code in "includes/strs.php":

$_TXT_NOTLOGGEDIN = "You are not logged in. Click <a href=\"?id=usercp/login\">here</a> to log in.";
$_TXT_USRTYPE_ADMIN = "Administrator";
$_TXT_USRTYPE_NORMAL = "User";

 

And here's where index.php includes the other two files:

require_once('includes/strs.php');

if (!isset($id)) { $id="home"; };
$file = "pages/".$id.".php";
if (!file_exists($file)) {
$file = "pages/404.php";
};
include($file);

 

Well everything works until you try to go to a page that is in a folder inside "pages". in this case, "pages/usercp/users.php" or "?id=usercp/users.php"

 

i get this error here:

Notice: Undefined variable: _TXT_NOTLOGGEDIN in C:\wamp\www\public\kodieg\pages\usercp\users.php on line 8

Notice: Undefined variable: _TXT_USRTYPE_ADMIN in C:\wamp\www\public\kodieg\pages\usercp\users.php on line 16

Notice: Undefined variable: _TXT_USRTYPE_NORMAL in C:\wamp\www\public\kodieg\pages\usercp\users.php on line 18

 

 

I'm not sure if i'm being descriptive enough, any questions just ask, but i feel like i've tried everything.

 

Anybody know why it's doing this?

 

Thanks.

So, does includes/strs.php have php tags so that those lines are seen as php code?

 

What is the actual code in pages\usercp\users.php from line 1 through line 18 that is responsible for the errors?

 

Without posting the relevant code, you're asking us to drive blind down the highway and to tell you what color of car we just ran into  ::)

includes/strs.php does have php tags.

 

and are lines 1 through 18 in pages/usercp/users.php:

<? include("user_auth.php"); function showpage() { ?>
<div align="left"><? include("user_nav.php"); ?></div>
<br><br>
<b>Current Users:</b><br><br>
<table>
<tr><th>Username</th><th>User Type</th><th><th></tr>
<?
echo $_TXT_NOTLOGGEDIN;
//Connect to database.
mysql_connect("localhost", "kodieg", "pass") or die(mysql_error());
mysql_select_db("kg_usersystem") or die(mysql_error());
$result = mysql_query("SELECT * FROM user_data") or die(mysql_error());

$logininfo = split(":", $_COOKIE['logininfo']);
while($row = mysql_fetch_array($result)){
if ($row['type'] == "1") { $user_type = $_TXT_USRTYPE_ADMIN; };			//Find out
if ($row['type'] == "2") { $user_type = $_TXT_USRTYPE_OP; };		//what type
if ($row['type'] == "3") { $user_type = $_TXT_USRTYPE_NORMAL; };		//of user

 

 

The posted code works for me - using a URL of http://mydomain/index.php?id=usercp/users gives - You are not logged in. Click here to log in. where the echo $_TXT_NOTLOGGEDIN; is at.

 

Either you are browsing directly to the users.php file or some of the code you have but have not posted is un-setting the variables, or you are actually doing something that you are not showing, like using a http request (index.php?id=http://yourdomain.com/usercp/users) instead of a file system path to include the file.

That's really werid...

 

I'm not browsing directly to the users.php file and there isn't really anything that i haven't posted.

 

Do you think it could be that i'm using WampServer?

 

I don't actually have any webspace so i use WampServer so that i can still code php on my computer.

Edit to the above: Upon further review, for the testing I did, I eliminated the function showpage() { code so that the code inside of the function was executed as main-scope code.

 

You have a variable scope problem. The code and data inside of a function is local to that function (which is the whole purpose of writing functions.) You need to pass those variables into the function as parameters or you need to reconsider and reorganize your code. What you are trying to make into a function is probably main-scope application code and does not belong inside of a function.

PFMaBiSmAd, your a genious. I removed the showpage() code to see if that would work and sure enough it did.

 

Now the reasoning behind the whole showpage() function was in user_auth.php it detects if the user is logged in or not. If the user is not logged in, it displays a message. If the user is infact logged in, it calls the showpage() function, which, shows the page.

 

Heres what's inside of user_auth.php:

<?
$restricted_pages = array(

"usercp/users" => "1",
"usercp/users/edit" => "1",
"usercp/users/delete" => "1",

);

if (!isset($_COOKIE["logininfo"])) { 
echo $_TXT_NOTLOGGEDIN; 		//If cookie does not exsist, then the user is not logged in, show message.
} else { 
if ($_COOKIE["logininfo"] !== "") {
	$logininfo = split(":", $_COOKIE['logininfo']);		//If user is logged in, split up cookie info "user:pass".

//Connect to database.
mysql_connect("localhost", "kodieg", "pass") or die(mysql_error());
mysql_select_db("kg_usersystem") or die(mysql_error());
$row = mysql_fetch_array(mysql_query("SELECT * FROM user_data WHERE name='$logininfo[0]'")) or die(mysql_error());
	if ($logininfo[1] == $row['pass']) {		//Check if password is correct.
		if (array_key_exists($_GET['id'], $restricted_pages)) { 		//Check if current page is restrcited to certain users only.
			if ($row['type'] == $restricted_pages[$_GET['id']]) { 		//If current page is restricted, make sure user has the authroity to view it.
				showpage(); 		//If user has authroiry to view the restricted page, allow it.
			} else { 
				echo $_TXT_RESTRICTED; 		//If user does not have authroirty to view the restricted page, deny it and show message.
			}; 
		} else { showpage(); };			//If page is not restricted, show it to any user.
	};
} else { echo $_TXT_NOTLOGGEDIN; };			//If the cookie exsists, but is blank, then the user is not logged in, show message.
};
?>

 

My next question is, in your opinion, what would be the best way to change this code so that i do not have to use the whole showpage() way?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.