Forum Moderators: coopster

Message Too Old, No Replies

strpos question

get string from other file

         

Jim123

10:41 am on Oct 11, 2010 (gmt 0)

10+ Year Member



I have 2 php files. One called fileA.php and one fileB.php. FileA.php is the code underneath.
I want to get the value in $string from fileB.php. If it is not, there I want to redirect to a page online called sorry.html.

Any idea how?

Thanks
Jim


 <?php
$string = "This is a strpos() test";
$pos = strpos($string, "This");
if ($pos === false) {
print "Not found\n";
} else {
print "Found!\n";
}
?>

Matthew1980

8:33 pm on Oct 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jim123,

1)read file using file_get_contents() <-this reads entire file to string, not array
2)apply your piece of code
3)result should be as you request...

if(file_exists("yourfile.php")){
$YourString = file_get_contents("yourfile.php");

$pos = strpos($YourString, "This");

if($pos === false){
//redirect as instance not found
header("location: sorry.html");//<--use the header function to redirect
exit;//<--use the exit to kill the script, good practise after a header() call
}
else{
echo "Woo! We matched the value we wanted!\n\r";
exit;//<--stops repetition of html sometimes, though completely optional
}
}
else{
echo "Sorry, no file found..";
exit;
}

Pseudo code, but the logic is there, I am tired so could contain error's but hopefully you get the idea. There are other ways, but this is the first that springs to mind.

Cheers,
MRb

Jim123

10:14 pm on Oct 11, 2010 (gmt 0)

10+ Year Member



I certainly got the idea. Thanks!

Jim123

2:58 am on Oct 19, 2010 (gmt 0)

10+ Year Member



I have tried it but I can't get it working...

I get the message: Sorry, no file found..

The file is a php file and I have used the direct path to it.

Any ideas?

enigma1

6:45 am on Oct 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Probably you haven't placed the yourfile.php with the text in it, try the original code see if it works.

<?php
$string = "This is a strpos() test";
$pos = strpos($string, "This");
if ($pos === false) {
header("location: http://www.example.com/sorry.html");
exit();
} else {
print "Found!\n";
}
?>

just replace the www.example.com with your domain

Jim123

8:49 am on Oct 19, 2010 (gmt 0)

10+ Year Member



No, it is not working.
I'm using this in one file
if(file_exists("http://localhost/test/wp-content/themes/resep/footer.php")){
$YourString = file_get_contents("http://localhost/test/wp-content/themes/resep/footer.php");
$pos = strpos($YourString, "teststring");
if ($pos === false) {
header("location: http://localhost/test/sorry.html");
} else {
print "Found!\n";
}
}


and this is the other php file where the teststring is
<div class="clear"></div>
</div>
</div>
<?php include (TEMPLATEPATH . '/bottom.php'); ?>
<div id="footer">

<div class="fleft">
Design: <a href="http://mmohut.com/">MMORPG</a> <br/>
Copyright &copy; <?php echo date('Y');?> <?php bloginfo('name');?><br/>

</div>

<div class="fright">

<a href="<?php bloginfo('rss2_url'); ?>">Subscribe to Posts</a> <br/> <a href="<?php bloginfo('comments_rss2_url'); ?>">Subscribe to Comments</a>

</div>
<div class="clear"></div>
</div>

teststring
</body>
</html>

enigma1

9:10 am on Oct 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The file_exists function may not work for urls. Use the fsockopen instead or the curl library to access external urls. If they're relative to your domain then use the paths from the root, eg:

/test/wp-content/themes/resep/footer.php

Jim123

10:08 am on Oct 19, 2010 (gmt 0)

10+ Year Member



Still no luck. Could it be something else? I´m using Wordpress. Maybe the footer.php must load before the other file?

I have removed the if(file_exists line, but no luck.
$YourString = file_get_contents("http://localhost/test/wp-content/themes/resep/footer.php"); has to be with http otherwise I get lots of errors.

enigma1

11:18 am on Oct 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So right after you load the $YourString var just echo it and see if the file contents show up.

The other thing is typically with sub-folders there are restrictions in effect. In other words you cannot access directly files in sub-folders files from another server or the footer.php itself may have a condition not to run unless the application runs as a whole.

So see if your code works with a different file, load it from the root, use a simple text file like test.txt and add few strings in it.

Jim123

10:08 pm on Oct 19, 2010 (gmt 0)

10+ Year Member



I'm going to give up

I created a txt file. Put it in every folder + the root.
Used this to called it
$YourString = file_get_contents("test.txt");
echo $Yourstring;


Output nothing...

Matthew1980

10:18 pm on Oct 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jim123,

Don't give up, there must be a simple explanation for why it isn't working.

In a single directory place your php file AND your text file. Double check that the file names are correct and that the permissions are correctly set (make sure that the text file can be read as well as written to).

Use the code that was posted and strip it down to file_get_contents() and literally place the echo in front of it.

IMPORTANTLY set the error_reporting(E_ALL) like this:

<?php
error_reporting(E_ALL);
echo file_get_contents("test.txt");
?>

OR

<?php
error_reporting(E_ALL);

echo "<pre>";
print_r(file("test.txt"));
echo "</pre>";
?>

Either of these versions will print the contents to screen...

If the files are in the same directory and they have permissions set right, that should output the contents of your test.txt to screen.

If not, then there is something else wrong... Not sure what as yet, but hopefully that won't occur.

Cheers,
MRb

Jim123

10:51 pm on Oct 19, 2010 (gmt 0)

10+ Year Member



First test looks promising. Text was printed.

I'll take it one step further.

Thanks (both of you)

Jim123

10:54 pm on Oct 19, 2010 (gmt 0)

10+ Year Member



Didn't work with footer.php. I got this error:
Warning: file_get_contents(footer.php) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\test\wp-content\themes\resep\header.php on line 45

Line 45 is the echo command.

<div class="clear"></div>
</div>
</div>
<?php include (TEMPLATEPATH . '/bottom.php'); ?>
<div id="footer">


<div class="fleft">
Design: <a href="http://mmohut.com/">MMORPG</a> <br/>
Copyright &copy; <?php echo date('Y');?> <?php bloginfo('name');?><br/>
<?php $foot = get_option('rsp_foot'); echo stripslashes($foot); ?>
</div>

<div class="fright">
<a href="http://mmohut.com/social-games">Facebook Games</a> | <a href="http://www.hostv.com/">VPS Hosting</a> | <a href="http://www.cirtexhosting.com/">Website hosting</a> <br/>

<a href="<?php bloginfo('rss2_url'); ?>">Subscribe to Posts</a> <br/> <a href="<?php bloginfo('comments_rss2_url'); ?>">Subscribe to Comments</a>

</div>
<div class="clear"></div>
</div>

<?php wp_footer(); ?>
</body>
</html>


What's the difference?

[edited by: Jim123 at 11:02 pm (utc) on Oct 19, 2010]

Jim123

10:58 pm on Oct 19, 2010 (gmt 0)

10+ Year Member



Btw, I'm trying to find out a way to protect my footer. An other way than encryption.

My thought was that if I put a keyword wrapped in php script in the footer and another file checks if the keyword is still present in the footer, load the website or else, go to an error page.

Maybe there is a simpler way, but the idea isn't that bad...

Matthew1980

7:31 am on Oct 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jim123,

>>Btw, I'm trying to find out a way to protect my footer. An other way than encryption.

Set a constant in the main parsing file, then check for it in the footer file, if it's not there, redirect the nosey person to an "Access this website in the correct way please", or do an instant redirect to the index page:-

(index file)
define('SECURITY', "1");

(footer file)
if(!defined(SECURITY)){
//redirect or display a message as file is being accessed directly
exit;
}

WRT the file, well at least your narrowing the cause down, I haven't come across that before, but I suspect that you will have to see where your text file is in relation to the script when executed.

[EDIT:]Lol, just noticed why your other post wasn't working:-

$YourString = file_get_contents("test.txt");
echo $Yourstring;<--This isn't an uppercase 'S' for the word String!

Getting there now!

Cheers,

MRb

Jim123

7:54 am on Oct 20, 2010 (gmt 0)

10+ Year Member



LOL you are right! And this is how we spend our time...

The other option is also a possibility that I am going to explore.
In my case I should do it the other way around; I have to define the 'SECURITY' in the footer coz I want to protect the footer.

Going to explore the 2 options.

Jim123

11:34 am on Oct 20, 2010 (gmt 0)

10+ Year Member



This works

<?php define('SECURITY', 1); ?>
<?php
if (defined('SECURITY')) {
}
else { ?>
<meta http-equiv="refresh" content="0;URL=http://www.google.com" />
<?php } ?>


Thanks for suggestion and all the time you've put in it.

Is there any chance I can get it to work when this is in the footer.php
<?php define('SECURITY', 1); ?>
and the other code in a file that loads before the footer.php?

I had to use an alternative to the header(location... coz I got an error.

Matthew1980

12:37 pm on Oct 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jim123,

The entire point of setting the constant (I presume as your setting it in the index file/main file) is that once it has been set, it is available throughout the scope of your script:-

define('SECURITY', 1);<--Define/set the constant here

//now include your files
include("your/files/here/footer.php");

In this example, as you have defined the constant BEFORE the include, the natural flow of the document will mean that because there is an include() there 'fetching' the data included within that file, that the SECURITY constant will be available there anyway, there is no need to redefine it as this will also generate a warning if you have error_reporting on.

And from the previous excerpt of code:-

if(!defined('SECURITY')){ <--this states: If SECURITY isn't defined throw an error, there is no need for the else clause
//case true
}

Lastly, try not to use <meta http-equiv="refresh" content="0;URL=http://www.google.com" /> as this can just be done with: header( "refresh:5; url=http://www.google.com"); and then just place an exit after the header call, and your away!

I hope as that makes sense to you anyway.

Good luck,

MRb

Jim123

1:19 am on Oct 21, 2010 (gmt 0)

10+ Year Member



Hi Matthew,

Thanks for the replies. This is very helpful for me as I understand php a bit more.

The reason why I used the meta is that I get the error when I use header:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\wp-

content\themes\resep\header.php:2) in C:\xampp\htdocs\test\wp-content\themes\resep\footer.php on line 27

So, I had to look for an alternative and the only one I could find was the meta tag. (I don't want to change anything in the php.ini).

The whole thing would be perfect if I could define the constant in the footer.php and check if it is set from a file

that is loaded earlier than the footer.php, let's say index.php.

The thing is that people want to change the footer. So when I encrypt it, they just look in the source code and copy/paste whatever they see in the footer part. When there is something defined in the footer and that is checked in some other file (and is also encrypted together with some code to make the website run) it is harder to crack. And if there is nothing defined in the footer, the website should stop working.

Hope you understand a bit what I am trying to accomplish.

Is there a way to read the defined part in the footer.php BEFORE I check it in the index or any other file before the footer.php?

Thanks
Jim

Matthew1980

9:50 am on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jim123,

If I am understanding you correctly WRT constants, they need to be defined first and in the flow of the document so that they have been assigned to memory, so in essence they can't be set in reverse, though you can do this sort of thing with class definitions weirdly, I digress.

Your header call, just check what's at those line numbers that are quoted in the error message to see what's there, as all it might be is white space or just failure to place an exit.. Not sure I would need to see the corresponding lines of code before I can 'assume' things ;)

Well, I hope that your having fun with the project now anyway! Certainly a good learning curve.

Cheers,
MRb

Jim123

10:33 am on Oct 21, 2010 (gmt 0)

10+ Year Member



Hi Matthew,

Yeah, it makes sense that the constants have to be defined first. I'm going to find some more about class definitions later on.

Line 2 in the header.php says:
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
. Can be done differently.

Thanks a million for all the help. I know how time consuming answering questions on a forum is; I do it myself as well...

Jim