php: failing when including a file that is defined in an array - php

I want to include a file, based on a value I get from my database. Inside class page in page.php I have this function to get the data I want :
function getPage(){
$page=array(
'title'=>$this->title,
'content'=>$this->content,
'module'=>$this->module
);
return $page;
}
And in pageview.php I call it like this : $elements=$page->getPage();
The function works just fine and I get my content , which I can manipulate like this for example(I know heredox is not the way to go but please ignore it, it's not the problem) :
echo <<<EOT
<div class='row-fluid'>
<H1> title:$elements[title]</H1></br>
$elements[content]
$elements[module]
</div>
EOT;
Now here comes the problem. I have a function called includeModule, which is like this :
function includeModule($page){
$page=strtolower($page);
if(file_exists("modules/".$page."php")) include("/modules/".$page."php");
else Echo "No such Module Exists :".$page." </br>";
}
Now lets say I want to include a page named "tax.php". If I use include("/modules/tax.php)"; it works just fine. If I try though to use include("/modules/".$elements[module].".php)"; it does nothing ( an yes $elements[module] does contain only the word "tax" ).
I even tried assigning the value of $elements[module] to another variable but nothing.
The strangest part of all is that if I try to use my function ( includeModule), even if I manually set the $page variable to "tax", it still doe not include anything and says that the file does not exist ( even though it does) ?
Any help on this ?
edit: i already tried removing the / but if i do it includes nothing again
edit: i've change the function a litle so i can get some feedback
if(file_exists("modules/".$page.".php")){
echo "<p>file exists</p>";
include("modules/".$page."**.**php");
}else{
echo getcwd();
Echo "<p>No such Module Exists :".$page."</p>";
}
OK solved. thanks for all the answers. By my mistake the trailing slash as well as the dot right before the php extension were left out . thanks for helping :)

if i use include("modules/tax.php)"; it works just fine.
In your code, you're using
include("/modules/".$page."php");
^-- Note the leading /
Try removing the /

You are seeing if "modules/".$page."php" exists but trying to include "/modules/".$page."php" which are probably not the same things (notice the leading / on the latter).
if(file_exists("modules/".$page."php")) include("modules/".$page."php");

OK ive solved it thanks guys:) each answer was helpfull :) ( the answer was a combination of your answers+a litle more atttention needed from me . I forgot the . right before the php extension .. lolz.. guess when you are tired you have to take a litle break off word in order to be able to concetrate right ). thanks again guys :)

Related

How do i put a $ before a database tag

I try to make a website with an account system, but now i am stuck.
I already tries to do it with this code <?php echo "<p>$</p>"$userRow['username']; ?>
what i want to have is that there will be something like this $USERNAME and that has to transform into the thing that i put into my config file.
$daan0605 = ('CoOwner'); $mohagames205 = ('HeadCreator'); Sorry that i dont know all the terms yet,
but i hope you guys can help me :).
Variable variables is what you're looking for. In that case you would simply do,
<?php echo $$userRow['username']; ?>
So if $userRow['username'] outputs daan0605 and $daan0605 = 'CoOwner';, then the above statement would output CoOwner.

PHP session changes unexpectedly in if statement and ignores echo command

I'm setting $_SESSION['showroom'] to 'active' when a particular page in Wordpress is displayed:
if(get_the_ID()==6470||get_the_ID()==252){
$_SESSION['showroom']='active';
}
I then set 2 arrays of pages to check against. If the next page displayed is NOT in one of these arrays, $_SESSION['showroom'] gets changed to 'inactive'.
$allowed_templates = array('template-A.php',
'template-B.php',
'template-C.php',
'template-E.php',
'template-G.php');
$allowed_ids = array(6470,252);
$template_name = get_page_template_slug();
$page_id = get_the_ID();
if(in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false){
$_SESSION['showroom']='inactive';
}
The if statement works most of the time, but sometimes my $_SESSION['showroom'] changes to inactive EVEN though one of the arrays is returning true! After several hours of testing I am unable to locate where the problem is. Echoing out the two parts of the if statement ALWAYS gives me 2 trues or 1 true + 1 false, but never 2 falses:
if(in_array($template_name,$allowed_templates)==false){echo 'TFALSE';}
if(in_array($template_name,$allowed_templates)){echo 'TTRUE';}
if(in_array($page_id,$allowed_ids)==false){echo 'IFALSE';}
if(in_array($page_id,$allowed_ids)){echo 'ITRUE';}
What am I missing here?
Thanks in advance for any help!
EDIT: Have continued testing and found the following anomaly:
if(in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false){
$_SESSION['showroom']='inactive';
echo 'SET TO INACTIVE';
}
The if statement changes $_SESSION['showroom'] to 'inactive' but DOES NOT echo out 'SET TO INACTIVE'!
There's something strange going on here!
Problem solved. My code was fine. Two missing images files were causing WordPress to crash my sessions. Took 10 hours to find out but happy I found it. Thanks to everyone for their help.
You can try the following;
if(!in_array($template_name,$allowed_templates) && !in_array($page_id,$allowed_ids)){
$_SESSION['showroom']='inactive';
}
Edit: lets try and break it down further... similar to your examples
if(!in_array($template_name,$allowed_templates){
echo "not in templates,";
}
if(!in_array($page_id,$allowed_ids)){
echo "not in ids,";
}
if(!in_array($template_name,$allowed_templates) && !in_array($page_id,$allowed_ids)){
echo "not in both\n";
}
then see if we get a result with not in templates,not in ids, but no trailing not in both
The problem is pure logical. Lets look at this statement:
if (in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false)
Which translates to "If the template is not valid AND page is not valid"
This means that both statements needs to be fulfilled in order to mark session as inactive. What if the template is fine, but the page is not valid? That definitely should be marked as inactive as well.
By changing the statement to read "If the template is not valid OR page is not valid", we cover up the invalid cases. Because either of them counts as an invalid state, and thus, only one of them needs to be false in order for everything to be false. (the OR-statement)
So code-wise it would be
if (in_array($template_name,$allowed_templates)==false || in_array($page_id,$allowed_ids)==false)
And you are set.
As and addition. I would structure the code as you noted works. Which is more logical. That is, mark it as inactive whenever it's should be treated as inactive, in all other cases mark it as 'active'. Or vice-versa.

Using WildCard in PHP if search program

this is my code that I've written so far --
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$search_link = "http://www.example.com/register_complete.php?user=";
if($actual_link == $search_link)
echo "Place your conversion pixel code here";
?>
The situation is like this - Once a user completes a registration he/she lands on urls like these -
http://www.example.com/register_complete.php?user=abcd1234
http://www.example.com/register_complete.php?user=abcd12345
http://www.example.com/register_complete.php?user=abcde123456
So what I am trying to achieve is fetch the current url using $_SERVER and then matching it with my sample url which is stored in $search_link if both these match then I wish to display a particular code otherwise the code will not be displayed anywhere else in the website.
I don't know how to use wildcard entries in PHP :(
My mind tells I should have done something like this -
http://www.example.com/register_complete.php?user=*
Can anyone over here help me regarding this please?
Why so complicated? I bet you can exclude http://www.example.com/register_complete.php, because if it's not this file, it won't load the script anyways, so focus on ?user=abc
if (isset($_GET["user"])) {
echo "Place your conversion pixel code here";
}
You can also try empty()
OR if you want to compare it to certain value:
if ($_GET["user"] == "abcd1234") {
echo "Place your conversion pixel code here";
}
There is a function which could help you with your problem and that is strpos(). Check out the following link:
http://in3.php.net/strpos

Getting value from session PHP

I am not sure why the variable username is not being returned in the session. When the user logs in, I start the session:
$username = trim($_POST['username']);
if(!isset($_SESSION)){ session_start(); }
$_SESSION[$this->GetLoginSessionVar()] = $username;
On the user's welcome page, when I run the echo command, I see the proper variable being returned. But I'm not sure why the return statement isn't working. I have the following in my PHP file:
function UserName()
{
return isset($_SESSION['name_of_user']) ? $_SESSION['name_of_user'] : "Unknown User" ;
//echo $_SESSION['name_of_user'];
}
In my html, I have:
Welcome back <?PHP $fgmembersite->UserName(); ?>!
I also checked the session ID, and it's also being generated properly.
Can you please help me understand what I'm doing wrong?
Is fgmembersite an object and have it the function called UserName ?
If yes, you simply miss an echo
<?PHP echo $fgmembersite->UserName(); ?>
You must add echo or print so should look like this;
<?PHP echo $fgmembersite->UserName(); ?>
You need to print out your variable. Use
Echo or print
Possibly you should add output:
<?php print $fgmembersite->UserName(); ?>
If you are using the script I think you are using, you need to look through fg_membersite.php at the line that says:
function CheckLoginInDB($username,$password)
whithin that line you should have a MySQL statement:
$qry = "SELECT etc...
When I tried to add UserAvatar I was able to do that by adding it to that MySQL string.
On a side note, I too am having trouble with adding UserName, and for the life of me I can't figure out why it would work any different than my previous workaround, yet somehow it is, but I am still convinced something in that file will do the trick eventually.
Edited:
Ok i got it, just do this:
echo $fgmembersite->UserName($username);
The username will pop right out. I have no idea why, i don't know enough php to explain it, but i can only assume this will get you going.

Possible to use PEAR SearchReplace to replace text within a .php file?

I came across this simple pear tutorial over here: http://www.codediesel.com/php/search-replace-in-files-using-php/
include 'File/SearchReplace.php' ;
$files_to_search = array("fruits.txt") ;
$search_string = "apples";
$replace_string = "oranges";
$snr = new File_SearchReplace($search_string,
$replace_string,
$files_to_search,
'', // directorie(s) to search
false) ;
$snr->doSearch();
echo "The number of replaces done : " . $snr->getNumOccurences();
The writer uses the fruits.txt file as an example.
I would like to do a search and replace on a .php file.
Basically what I am trying to achieve would be this:
On a user interaction, index.php is opened,
$promoChange = "%VARYINGTEXT%";
is searched for and replaced with
$promoChange = "$currentYear/$currentPromotion";
The $current variables will vary, hence the need to change the words inbetween the "" only.
Does anyone have any input on how this type of task could be accomplished?
If anyone knows of any tutorials relating to this subject, that too would be greatly appreciated.
Thank you!
I do have everything else figured out, regarding the template and user interaction, I am just having trouble trying to work out how to accomplish this type of search and replace. I have an understand of how it should be done as I have made something similiar using visual basic. But I am starting to this that my answer for this would be perl? I hope that this is not so...
Okay, my problem is partly solved with this:
// Define result of Activate click
if (isset($_POST['action']) and $_POST['action'] == 'Activate')
{
include ''.$docRoot.'/includes/pear/SearchReplace.php' ;
$files = array( "$docRoot/promotions/index.php" ) ;
$snr = new File_SearchReplace( '$promoChange = "";', '$promoChange = "'.$currentYear.'/'.$currentPromotion.'";', $files) ;
$snr -> doSearch() ;
}
but how do i get it to search and replace something like $promoChange = "%VARYINGTEXT%";
It found and replaced "" with the current session values. But now that is has changed, I need it to replace and text inbetween "AND".
Any ideas anyone?
If you only need to adapt a single file, then do it manually:
$src = file_get_contents($fn = "script.php");
$src = str_replace('"%VARYINGTEXT%"', '"$currentYear/$currentPromotion"', $src);
file_put_contents($fn, $src);
str_replace is sufficient for your case.
Why on earth do you want to do something like that? Frameworks like PHP do exist solely on the base of not having to write a page for each different view of the same interaction. What's wrong with just including the PHP page you now want to change, and set the variables accordingly before calling it?
Ontopic: I don't see why what you're doing is a problem, purely technically speaking. This can be done using PHP. But really, you shouldn't.

Categories