Page not reading query string without index.php in the URL - php

this problem started happening when my server got upgraded to use PHP7, so I suspect it has something to do with that.
My website's URLs build the page content based on the query string. For example:
example.com/?prop=one
example.com/?prop=two
example.com/?prop=three
etc...
But for some reason, when I do the following:
$prop = $_REQUEST["prop"];
or:
$prop = $_GET["prop"];
It does not recognize the specified query string value.
On the other hand, using the following URL format works fine (note the addition of "index.php"):
example.com/index.php?prop=one
example.com/index.php?prop=two
example.com/index.php?prop=three
In other words, I'm able to grab the prop query string value without a problem with the presence of the "index.php" in the URL. I don't want to use index.php in the URL, I'd like the URLs to remain clean. This is basically a single page info-app that changes the content via that query string value, so there should be no need for index.php at the root of the domain.
Any thoughts as to how I would fix this? Does this have something to do with updating to PHP7 on the server?

Well, this is embarrassing.
Basically I realized that the problem was that I had an index.html file at the root, and the server was picking that up by default when the index file wasn't specified. So naturally there was no PHP present, so it gave the impression that the query string value wasn't being read correctly. After renaming the index.html file, everything works as expected.
I'm wondering if the settings on my previous server were set to pick up the index.php file by default instead of index.html.
I'd delete this entire post but I suppose someone else might have the same problem so I'll leave it.

Related

What is the function of using php for site links?

I am working on a site and the builders have used a mix of php and html for links. For example:
<li>Variable Speed Drives</li>
<li>Corrosion Resistant Baseplates</li>
and
<li>MP Repair</li>
<li>MTA Repair</li>
The php is referenced in another file in this way:
<?php
$pdf_link = "../pdf/";
$external_pdf_link = "../../pdf/";
$video_link = "../video/";
$external_video_link = "../../video/";
?>
My concern is not knowing the function of the php, other than it being a placeholder, and given that the links work both ways, I don't want to break something because I am clueless to its purpose.
In doing my due diligence researching, I ran across this post, which is close, but still no cigar, Add php variable inside echo statement as href link address?. All of the research seems to be about how rather than why. This is the site, and they only used it for the "Downloads" links: http://magnatexpumps.com/
Thank you...
B
There is no right way. They are just different.
Let's forget the PHP for a while. If you have this link in a page:
<a href='about.html'/>About</a>
What will happen? The browser will change the URL of the document. If you are at the root of the site like: "www.example.com", will redirect to "www.example.com/about.html". If you are in a URL like "www.example.com/news/index.html" will redirect you to "www.example.com/new/about". That's why sometimes it is useful to have a variable before, to force a full path URL.
Another case of URL variable interpolation is when you have different systems running in the same url. In this case, you will have to append the system name in order to get to where you want. If you don't know where your application will run if it will run on the doc root, or in a subfolder, use a variable to indicate the base path.

Directory path calls, strange behaviour

I have an ajax request that I'm trying to call a specific file from which is located at:
ROOT/admin/functions/upload/filename.php
And the page making the request from is located at:
ROOT/admin/customers/123
Which is modified through htaccess from
ROOT/admin/customer.php?id=123
I have tried every combination of paths I could think of but I get some strange behaviours for example when I use
../functions/upload/filename.php
It looks for the file in
ROOT/functions/uploads/filename.php
And when I use
functions/upload/filename.php
It looks for the file in
ROOT/admin/customers/functions/uploads/filename.php
So I tried
../admin/functions/upload/filename.php
And it looks in
ROOT/admin/admin/functions/upload/filename.php
I'm pulling my hair out here, has anyone got any ideas as to what this might be?
Any help would be greatly appreciated.
Thanks, James.
Since the browser knows NOTHING about your server-side paths, and it only has the path you see in the address bar, e.g.
http://example.com/ROOT/admin/customers/123
then if your ajax code looks like
$.ajax('functions/foo/bar.php');
Then the ajax call will be requesting
http://example.com/ROOT/admin/customers/123/functions/foo/bar.php
Similarly, adding ../ just strips off levels of the source page's address:
$.ajax('../../functions/foo/bar.php');
results in
http://example.com/ROOT/admin/customers/123/../../functions/foo/bar.php
^-A-^
^-------B------^
http://example.com/ROOT/admin/functions/foo.bar.php
You probably want
$.ajax('/ROOT/functions/foo/bar.php');
With that leading /, the browser ignores ALL of the subdirectory stuff in the url and uses the entire path from the ajax call as the entirety of the path.
http://example.com/ + /ROOT/functions/foo.bar.php

get URL in php IN INCLUDE FILE

i have some problem i try to get the uri in php.
I'm using:
$_SERVER['REQUEST_URI']
It works just fine if i do it in the index.php, but, i NEED to get the url in a include file, but, when i do it, it takes the FILE adress, i mean, it shows something like this
adress bar: www.webpage.com/index.php
$_SERVER['REQUEST_URI'] output: webpage/includefile.php
I am explaining myself here? Thanks!
How are you including the file? If it's being included via an HTTP reference then it's actually being served as a page and the functionality you are seeing is correct. If the include path is a local file, you shouldn't be seeing this behaviour
Found this whilst trying to solve the same issue.
My solution that worked is to use $_SERVER['HTTP_REFERER']
This worked well in that it also included the parameters (e.g. ?this=that&foo=bar)
Maybe somewhere in your code (or in another include file) the value is overwritten.

How do you create domain.com/?stringhere without a .php extension

I want to create a link like the following:
http://www.myurl.com/?IDHERE
What i want to be able to do is be able to goto the above link, and then pull whats after the ? (in this case IDHERE) and be able to use that information to perform a MySQL lookup and return a page.
Can anyone point me into the right direction? please know this is using PHP not ASP
The issue here is not with your scripting language, but with your web server setup. I'll refer to these by their Apache names, but the features should be available in most web servers.
There are three features you might want to use:
1) content negotiation (mod_negotiation), which allows your web server to try a specified list of extensions in a specified order, for example: http://example.com/foo might be http://www.example.com/foo.html or http://example.com/foo.php
2) DirectoryIndex, which tells the web server that when a client asks for http://example.com it should look for a specified list of files in order, so it might server up http://example.com/index.html or http:/example.com/index.php
3) mod_rewrite, which allows you to basically rewrite the URL format received by the server. This allows you to do things like translate http://example.com/foo/bar/baz to http://example.com/foo/bar.php?page=baz
The rest is done by the backend script code as normal.
Create a default PHP file in that directory that will get loaded when no file name is specified (e.g. index.php). In your PHP script you can get the part after the question mark from the variable $_SERVER['QUERY_STRING'].
Do the following in your site's main index.php:
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
In the link, form or whatever - index.php?id=someid
In your index.php file:
$_GET['id'] = $id
Now you can use it:
e.g.
echo $id;
Since it's your default page, it will work without the extension.
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
this was exactly what i was looking for, though now i just need to create something to notify if nothing is there or not. Thank you all for your responses.
I would solve it by using .htaccess file if possible.
create a .htaccess file in the main directory with the content:
RewriteEngine on
RewriteRule cat/(.*)/(.*)/(.*)/$ /$1/$2.php?$3
that should translate "example.com/foo/bar/baz" to "example.com/foo/bar.php?page=baz"

PHP Permalinks.. how to change?

I built an client website with php script purchased recently and the support for script is pathetic, I want the permalinks of the script to be search engine friendly but they're not. They have spaces inbetween which are not at all indexed by search engine..
So, how can I change the permalinks? Thank you all..
If you've got 50 PHP files and an .htaccess file that come with this "script", you'll likely first have to find the programming path that flows through them. If you take a look at the .htaccess file, you should see some ModRewrite lines, which should end with a PHP file name. That's the script that's receiving (and decoding) the permalinks. That file would be a good place to start looking for a hook to rewrite the permalink structure. If you could post the source code (or put it somewhere like pastebin and post the link) for that file, I'd be glad to take a look.
From one of your other comments it sounds like at least part of the script is using the Smarty PHP template engine. If so, if you can find a folder that contains "cache", "templates", and "templates_c" folders (or similar), you can rule that one out as well; that would be the templates used to show the page, and not any of the decode/encode scripts.
EDIT: Looking at your .htaccess file, line 29 looks to the be one that deals with article permalinks, and it points to view.php, and converts what was the permalink into id and title GET variables. Post the source of view.php if you could, and we'll go from there.
EDIT 2 Okay, looking at view.php gained a little more insight. Primarily of which is that there is no decoding function; the Answerscript engine promptly discards the 'title' part of the permalink and only looks up a question by its ID (number following the pipe on the URL query; you can prove this on their demo page by changing the title part of the URL to anything else, and it will still fetch the right page). So, the good news is that there's no decoding function that needs to be updated when you change the encoding function. Unfortunately this does very little to tell us where the encoding function is in the scripts.
The only hint is that the view.php file includes a file called include/functions/import.php, which I'm presuming has function definitions for does_post_exist($PID), update_last_viewed($PID), update_your_viewed($USERID), and update_viewcount_question($PID). Let's see the source of that file to see if there's any other functions in there that would be used for importing. Also, how many files are in the include/functions/ folder? If there's only a few, post all their sources; likely the encoding function is defined in there. If there's a bunch, is there an export.php file in that folder (i.e. the opposite of import.php that was used by view.php)? Post that file's source as it likely has the encoding function.
EDIT 3 There they are: in the main.php file there's a trio of functions: seo_clean_titles, insert_seo_clean_titles, and seo_clean_titles2. insert_seo_clean_titles is a function to be called from within a Smarty template (search all files that have a .tpl extension for {insert name="seo_clean_titles" to see where that's used), and the difference between seo_clean_titles and seo_clean_titles2 is that the first echoes out the result, while the second returns it. However, all three have the line $title = str_replace(" ", "-", $title);, which should be turning all spaces in the title to hyphens. If you're not seeing that result, likely the code is not calling these functions at the right places. You can search through all the .php files and see if anywhere else there's a call to seo_clean_titles or seo_clean_titles2, and make sure the result is being actually used as the final URL.
Edit 4 To add ".html" to the end of all URLs: Here's the line in the template file linking to the question page:
{$ques[i].title|stripslashes}
Change that to:
{$ques[i].title|stripslashes}
and the links will have ".html" on the end. You'll then need to modify view.php to strip the ".html" back off again when parsing out the ID number: Right before $pid = intval($ph);, insert the following:
if (strtolower(substr($pid, -5)) == ".html") $pid = substr($pid,0,-5); // Remove ".html" if it exists
That should do it!
The urlencode function would take care of the spaces in the URL.
For example:
<?php
$base_url = 'http://example.com';
$category = urlencode('some thing');
$item = urlencode('Name of an item');
echo "<a href=\"{$base_url}/{$category}/{$item}/>{$item}</a>";
?>
This would make the link http://example.com/some+thing/Name+of+an+item/ which a search engine should be fine with.
And, assuming you are using URL rewriting (like mod_rewrite), the values should reach your PHP script as they were before the urlencode function. If not, you can revert them back with urldecode.

Categories