URL querystring with a php include - php

I'm trying to include a file to output in a tab on a page. The file itself will pull up just fine, but when I try to add the required querystring to it, it gives me a "failed to open stream: No such file or directory" error.
I've tried just a straight include and tried setting the querystring as a variable. Here's where I'm at right now.
$listingVars = '?mls=' . $_REQUEST['mlid'] . '&lid=0&v=agent';include("agentview.php$listingVars");
Has anyone successfully done this?

You can't include a query string in an include().
Assuming this is a local script, you could use:
$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");
if it's a remote script on a different server, don't use include.

I created a variable on the 2nd page - and passed it a value on the first page - and it worked for me:
*Page with include: 'index.php'
<?php $type= 'simple'; include('includes/contactform.php'); ?>
*Page included: 'includes/contactform.php'
switch($type){
case 'simple':
//Do something simple
break;
default:
//Do something else
break;
}

I modify the accepted answer given by Frank Farmer a bit to work for different queries:
Include twice would cause problem:
$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");
//changing the v to another
$_REQUEST['v'] = 'agent2';
include("agentview.php");
For those who encounter this multiple include problem, you could wrap you code inside "agentview.php" in a function:
Inside agentview.php
function abc($mls,$lid,$v){
...your original codes here...
}
file need to call agentview.php
include_once("agentview.php");
abc($_REQUEST['mlid'], 0, 'agent');
abc($_REQUEST['mlid'], 0, 'agent2');
Hope it helps someone encounter the same problem like me and thanks Frank Farmer for the great solution which saved me alot of time.

Related

$_GET in Wordpress function.php - Not working

First, thanks for the time. I have been messing with this quite a bit and cannot figure out what is going wrong (and this is something I thought I understood) I am currently trying to change save location of files uploaded via WP. As the title suggests. The $_GET is not pulling anything from -
/?action=type1&project=1&method=add
I can replace the gets with appropriate numbers and get correct project output so I am almost certain the $_GET that isn't pulling the info. As I understand this is a supervariable so should have no problems getting into a function.
Can anyone let me know if they see any problem in my logic, syntax or am I missing something going on in upload_dir/wp_handle_upload_prefilter hook? Or are there any suggestions on troubleshooting/solving?
$_GET is used everywhere on the site and works fine otherwise.
function ca_doc_pre_upload($file){
add_filter('upload_dir', 'ca_doc_custom_upload_dir');
return $file;}
function ca_doc_custom_upload_dir($path){
if (isset($_GET['project'])) {
$projectID = $_GET['project'];
};
if (isset($_GET['action'])) {
$type = $_GET['action'];
};
$project = ca_get_project($projectID);
$customdir = '/'.strtolower(str_replace(" ", "-", $project->project_name)).'/'.$type;
$path['path'] = str_replace($path['subdir'], '', $path['path']);
$path['url'] = str_replace($path['subdir'], '', $path['url']);
$path['subdir'] = $customdir;
$path['path'] .= $customdir;
$path['url'] .= $customdir;
return $path;}
add_filter('wp_handle_upload_prefilter', 'ca_doc_pre_upload', 2);
To Confirm not able to retrieve with get. Used.
if (isset($_GET['action'])) {
$type = $_GET['action'];
}else{
$type = 'Not Getting';};
And tried to save a file. It saved it to Not getting the folder. (took out the project part).
Some additional information. Created another function in function.php using get and called it on the same page where plupload happens and it worked. The only thing I can figure now it that this isn't working because its called from upload or the hook is doing something... I am only guessing now because I ran out of viable logic a long time ago.
Thanks again for the time.
Have you registered the query params with WP query_vars()? Perhaps the permalink rewriting is interfering..?
https://wordpress.stackexchange.com/a/41373
You may also need to re-save your permalink settings after you add the parameter.
With some other help and research turns out that plupload that handles the upload in Wordpress was doing some AJAX work in the background calling;
url: '<?php echo admin_url('admin-ajax.php')?>'
And for some reason the $_GETs were not going with that, so just changed the URL to include them as follows, and all was right again.
url: '<?php echo admin_url('admin-ajax.php?project='.$_GET["project"].'&action='.$_GET["action"].'') ?>'
Apparently when using the hooks above this is the point(admin-ajax.php) in which it grabs the $_GETS
have you tried
get_query_var('action');

Magento change language by login

I'm searching now for hours. I try to switch the store language after the login.
Given is:
The id of the Store I want to switch too.
The Event Observer is also done.
This is what I worked out the last hours
My Observer:
$customerId = Mage::getModel('customer/session')->getCustomer()->getId();
// Get the Store ID we want to switch too
$connection = Mage::getSingleton('core/resource')->getConnection('distributor_read');
$mainLanguage = $connection->fetchAll('SELECT...');
$storeId = $mainLanguage[0]["store_id"];
if (!$storeId == null) {
$storeCode = Mage::app()->getStore($storeId)->getCode();
// Here I have to switch by the store code
return;
}
Would be glad if someone could help me out.
At least I need a method to switch the language or storeview, but I don't find any working MagentoAPI methods.
to Set the store id programatically
In the index.php file, (in your language specific folder), add the following:-
$store_id = 'your_store_id_here';
$mageRunCode = 'store view code';
$mageRunType = 'store';
Mage::app()->setCurrentStore($store_id);
Mage::run($mageRunCode, $mageRunType);
I am suggesting you to create some temporary session variable from login action and read in index.php to set language pack and again unset it if your work has been done
Hope someone will find this information useful :)
I will tell you guys what I did for this case. I tried to get the Mage_Core_Controller_Response_Http class, completely in vain.
So I kept going on my research and I found a solution.
I used:
header('Location: '. Mage::app()->getStore()->getBaseUrl().'/customer/account?___store='.$storeCode);
There we go, my on login observer just set the language.
edit:
To set a new header could cause some problems, because if just any piece of html is already rendered you can not set a new header.
I worked sth. else out:
$url = Mage::getUrl('*/*');
$url .= "?___store=" . $storeCode;
$response = Mage::app()->getFrontController()->getResponse();
$response->setRedirect($url);
$response->sendResponse();
exit();
There is obviously still a problem, the exit shouldn't used in good software code, but a simple return does not work, it does not end or kill the observer action.
I still working on a solution to kill the observer in a right way. As I said the observer need to get killed to redirect the url.

How to detect what page user is on with php

I need to create some if else statements depending on what page user is, I tried looking for this in php manual, but didn't find anything useful.
Basically what I want is a syntax for something like:
if (user is on a page index.php)
$message = $_GET["title"];
if $message = "hello";
$say = "Hello";
etc ....
Can anyone show how this can be done?
Try looking at _SERVER[REQUEST_URI], _SERVER[SCRIPT_NAME], _SERVER[SCRIPT_FILENAME], _SERVER[PHP_SELF], and possibly others.
you can use a session variable to track the user: every page is opened set a session variable to it's name so you can know what is the last opened page
x = str_replace('.php', '', (end(explode('/',HttpRequest::getUrl))));

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.

Access control of page in php

I want to control the access in php website.
I have a solution right now with switch case.
<?php
$obj = $_GET['obj'];
switch ($obj)
{
case a:
include ('a.php');
break;
default:
include ('f.php');
}
?>
But when i have so many pages, it becomes difficult to manage them. Do you have better solutions?
Right now, i develop the application using php4. And i want to use php5. Do you have any suggestions when i develop it using php5?
Thanks
$obj = $_GET['obj'];
$validArray = array('a','b','c','d','e');
if (in_array($obj,$validArray)) {
include ($obj.'.php');
} else {
include ('f.php');
}
The more pages you have the harder it will be to control this.
Your best off using a framework of some sort, my personal preference is CodeIgniter.
why not just address a page itself?
first page
another page
I am not saying that this is the best solution, but years ago I used to have a website which used a database to manage the key, the page to be included, and some informations like additional css for instance.
So the code was something like:
<?php
$page = htmlspecialchars($_GET['page']);
$stuffs = $db->query('select include,css from pages where pageid = "' . $page . '" LIMIT 1');
?>
So when we needed to add a page, we just created a new field in the database. That let us close a part of the website too: we could have a "available = {0,1}" field, and if zero, display a static page saying that this page was under maintenance.

Categories