reading a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

reading a file

Post by nite4000 »

Can someone help with this?

I have a file that will have a domain name in it along with other info the domain would be like this http://www.mydomain.com

Now the script of course would chk to see if the file exists but will compare the domain name from file to the url/domain. I need to have it give a error if they dont match

remember the file would have other info so it would have to look for the domain name/

I have tried everything but nothing seems to work for me here is code i have

Code: Select all

<?php 

$section = file_get_contents('file.php', NULL, NULL, 0, 50);

if($pageURL !=$section) {
	echo 'error';
	
$pageUR1  = ereg_replace("/(.+)", "", $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); 
} else { 


echo $pageUR1;
}

?>

hope someone can help.
dlaurent86
Forum Newbie
Posts: 5
Joined: Sun Aug 22, 2010 5:54 pm

Re: reading a file

Post by dlaurent86 »

Hey buddy i do see one problem with your script you declare the $pageUR1 variable inside the if statement try this:

Code: Select all

<?php 
$section = file_get_contents('file.php', NULL, NULL, 0, 50);
$pageUR1  = ereg_replace("/(.+)", "", $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); 

if($pageURL !=$section) {
        echo 'error';  
} else { 
echo $pageUR1;
}
?>
another thing is i would use file.txt instead of file.php if your just using the file to store your information as a string. and instead of storing your data in a file why not use a database.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: reading a file

Post by nite4000 »

it didnt work. I changed it to a txt file and when i put the domainname in teh file and ran it on the same domain i still get a error msgs from the code.
it should be ok since its the same domain but its not
dlaurent86
Forum Newbie
Posts: 5
Joined: Sun Aug 22, 2010 5:54 pm

Re: reading a file

Post by dlaurent86 »

ok do me a favor run this script and let me know what you get.

Code: Select all

<?php 
$section = file_get_contents('file.txt', NULL, NULL, 0, 50);
$pageUR1  = ereg_replace("/(.+)", "", $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); 
echo 'section: '.$section.'<br/>pageUR1:'.$pageUR1;
?>
obviously $section and $pageUr1 do not match. so you need to change the url in file.txt
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: reading a file

Post by nite4000 »

when i ran that i got this

section: www.mydomain.com
pageUR1:www.mydomain.com

The domain i am using i dont want to give out but they were the same when i ran it.
dlaurent86
Forum Newbie
Posts: 5
Joined: Sun Aug 22, 2010 5:54 pm

Re: reading a file

Post by dlaurent86 »

ok my apologies the first code i gave you the variable names were incorrect. try this and respond

Code: Select all

 <?php 
$section = file_get_contents('file.txt', NULL, NULL, 0, 50);
$pageURL  = ereg_replace("/(.+)", "", $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); 

if($pageURL !=$section) {
        echo 'error';  
} else { 
echo $pageURL;
}
?>
and looking at your original code.

Code: Select all

<?php 

$section = file_get_contents('file.php', NULL, NULL, 0, 50);

if($pageURL !=$section) {
        echo 'error';
        
$pageUR1  = ereg_replace("/(.+)", "", $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); 
} else { 


echo $pageUR1;
}

?>
you where declaring $pageUR1 inside the if statement comparing $pageURL to $section. im assuming $pageUR1 and $pageURL are the same variable and there was just a typo.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: reading a file

Post by nite4000 »

ok it works from what I can tell. Thank you for your help. I knew I was close just wasnt catching it. Thanks again.
Post Reply