How to get the parameters from url without notice - php

So I was thinking of a way to remove the parameters from url when page is downloaded to client and respond different in each different value of the get parameter.
Let me take it clearer for you. Say I have this url: www.abc.com/?q=jsdfnjns. Ideally, I am thinking of shortening this url with goo.gl and then send it to the customer. When customer clicks on it, it will automatically go to www.abc.com clean url and set a cookie to the client with the q's value. I have seen it before in many affiliate links except that the initial url had no get parameters but value was actually a sub-folder e.g www.abc.com/jsdfnjns
So what's the way to actually get the value of a get parameter and manipulate it with php, while removed from the url without user's notice, or setting a cookie when parameter is given as a sub-folder. I suspect it must be some htaccess rules and php tricks but can't find a way.
With given url www.abc.com/jsdfnjns how can i redirect immediately to www.abc.com
and have the jsdfnjns saved ideally server-side in apache or in a user cookie ?
Is there any way to make it also happen with actual get parameters too ?
And a schematic:
www.abc.com/jsdfnjns convert it to -> goo.gl/sjbjsb -> when clicked, user is going to www.abc.com but somehow i get the jsdfnjns and respond in the main page different.
Hope my question is well defined, any ideas will be appreciated.
Thanks.

Firstly you need to set .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?q=$1 [QSA,L]
</IfModule>
index.php code:
session_start();
if(isset($_REQUEST['q'])) {
$_SESSION['q'] = $_REQUEST['q'];
header('Location:index.php');
die();
}
else{
if(isset($_SESSION['q'])) $q = $_SESSION['q'];
else $q = null;
//YOUR CODE
var_dump($q);
}

Related

Have user query www.example.com/parameter and use that parameter without changing URL in url bar

So basically I have a website with user profiles. I want users to be able to query a profile like this:
www.example.com/john
and that would trigger another page I've written in PHP to display that profile but WITHOUT then changing the url. I've looked at other examples on SO like this:
How do I achieve a url like www.example.com/username to redirect to www.example.com/user/profile.php?uid=10?
However this uses url rewriting which means the URL would then change. For instnace if the page is called users.php, I don't want this to happen:
user queries www.examples.com/john -> page is changed to www.examples.com/users.php?name=John
I want the url to stay as www.examples.com/john but for the server to serve up the page www.examples.com/users.php?name=John
Would I still use url rewriting to achieve this even though I don't want the url to change in the url bar? Hope someone can help, thank you!
Usually who needs this kind of feature uses Routers.
Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.
Basically you can take your url and divide it in parameters. The response it's related to the input url.
There are some good libraries in php which allows you to handle routers, for example:
Dispatch: https://github.com/noodlehaus/dispatch
Phroute: https://github.com/mrjgreen/phroute
In phroute you can solve your problem just with:
$router->get(['/user/{name}', 'username'], function($name){
//retrieve $name information
return 'Hello ' . $name;
})
Just for information, every MVC framework uses router as standard.
Using .htaccess File
RewriteEngine on
RewriteRule ^/([a-zA-Z0-9])$ /users.php?name=$1
I hope this will solve your issue, now what it will do, on the front or to the public url is like http://www.example.com/john but users.php script on your server will receive a GET parameter name which will have the value john.
Just make sure apache mod_rewrite is turned on.
With Apache's rewrite module, you can do a rewrite or a redirect.
Redirect means, send a response (status code 301, 302) to the client and ask to fetch another URL. This causes the browser's URL bar to change to the new URL. This happens, when you add the R|redirect flag to a rule, e.g.
RewriteRule ^foo$ /bar.html [R,L]
or when the target is an absolute URL containing a domain, e.g.
RewriteRule ^foo$ http://www.example.com/bar.html [L]
Rewrite on the other side, means take the request and send an answer somehow, without redirecting the client to another resource.
RewriteRule ^foo$ /bar.html [L]
This time, the client asks for foo, but the server sends the contents of bar.html without telling the client.
So, as long as you don't add the R flag, or give an absolute URL, the client's URL bar won't change, e.g.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^john$ /users.php?name=john [L]
or more general
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /users.php?name=$1 [L]

How to allow a user "the option" to create own URL in PHP

Using mod_rewrite to use a $_GET['variable'] to grab info / pages is easy enough. How do you give a user the option to do this. For example: There URL is blah.com/user?id=74378 by default. Now they can manually create there own URL if available. How is this done? Thank you
You could maintain a table with the list of urls used. This is to check and warn the user if url is unavailable. Then, if the user tries blah.com/user/myspecialurl the htaccess should call blah.com/user?url=myspecialurl. Then use the table to find the userid and get the contents using the GET variable.
All this is under the assumption that you have a fixed format/restrictions for the url
The first part, anything, on this page will help where anything = user defined string and u process that string as a GET variable on a fixed page.
http://www.sitepoint.com/guide-url-rewriting-2/
use php module mod_rewrite, and rules in htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
and in index.php:
$page = isset($_GET['page']) ? $_GET['page'] : 'index';
// filter var $page
// ..
// then include needed page
include './pages/'.$page;
// or get content from sql
sql(..WHERE page = $page..)

Creating dynamic URLs in htaccess

I'm trying to write an .htaccess file that will make my URLs more attractive to search engines. I know basically how to do this, but I'm wondering how I could do this dynamically.
My URL generally looks like:
view.php?mode=prod&id=1234
What I'd like to do is take the id from the url, do a database query, then put the title returned from the DB into the url. something like:
/products/This-is-the-product-title
I know that some people have accomplished this with phpbb forum URLs and topics, and i've tried to track the code down to where it replaces the actual URL with the new title string URL, but no luck.
I know I can rewrite the URL with just the id like:
RewriteRule ^view\.php?mode=prod&id=([0-9]+) /products/$1/
Is there a way in PHP to overwrite the URL displayed?
At the moment you're wondering how to convert your ugly URL (e.g. /view.php?mode=prod&id=1234) into a pretty URL (e.g. /products/product-title). Start looking at this the other way around.
What you want is someone typing /products/product-title to actually take them to the page that can be accessed by /view.php?mode=prod&id=1234.
i.e. your rule could be as follows:
RewriteRule ^products/([A-Za-z0-9-])/?$ /view.php?mode=prod&title=$1
Then in view.php do a lookup based on the title to find the id. Then carry on as normal.
One way to do it, would be just like most mvc frameworks. You can redirect all your pages to the same index.php file, and you use your script to determine which page to load.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
and your php file will have a script like this one:
// get the url
$uri = (isset($_SERVER['REQUEST_URI']))?$_SERVER['REQUEST_URI']: false;
$query = (isset($_SERVER['QUERY_STRING']))?$_SERVER['QUERY_STRING']: '';
$url = str_replace($query,'',$uri); // you can edit this part to do something with the query
$arr = explode('/',$url);
array_shift($arr);
// get the correct page to display
$controller =!empty($arr[0])?$arr[0]:'home'; // $arr[0] could be product/
$action = isset($arr[1]) && !empty($arr[1])?$arr[1]:'index'; // $arr[1] can be product-title
}
of course you will have to work this code to fashion your application
I hope this helps
One way would be to output a Location: header to force a redirect to the chosen URL.

How do I remove a variable name from a URL using htaccess?

So basically I want users to be able to go to my website with a URL of something like /45678, instead of having to use /?p=45678, so really I just want to remove the variable name. I've tried using mod_rewrite, but it seems that is only for removing the name when the page is visited.
Here is the current code:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=([0-9]+=$
RewriteRule ^/$ /%1 [R]
Simply change all of your links to /45678 rather than ?p=45678. Or did I misunderstand you completely? Because what I got from your post is that it works properly, unless you manually access the ?p=45678 where as it stays as ?p=45678.
EDIT:
This is what I am using for http://www.madphp.org/dev/, give it a go, works like a charm for me (it also removes the index.php part). To access your now cleaner URL you would simply explode the $_SERVER['PATH_INFO'] variable to get all of the required parameters within your PHP script.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Have you set up mod_rewrite correctly? If so, you can use variables like simple $_GET variables, in this case you must access $_GET['p'] in PHP.
I did this without using .htaccess, but it does query a database. I wrote this a while ago so it uses PEAR DB, adjust to your database/connection method. I'll just copy my code and let you figure out what changes you need.
$db=connect_db();
$name=substr($_SERVER['PHP_SELF'], 20);
$name=strtolower($name);
$id=$db->getone("select id from user where login='{$name}'");
header("Location: /dragonart/profile?user=" . $id);
If you store your information in a database this may be a nice alternative. The downside is that the the URL is not rewritten and the user is ultimately sent to a page with ending in a $_GET variable.
edit:
Just realized that using my method a simpler method can be used for the answer. Since my solution was used to find the id of a user using their username and then send someone to their profile (which requires the id) a better solution would be something like:
$var=substr($_SERVER['PHP_SELF'], $length);
header("Location: /path/to/page?p=".$var);
where $length is the usual length of the URL without the variable at the end.

URL Rewriting: "index.php?id=5" to "home.html"

I'm developping a website using php and the "template.inc" class.
The problem is that I want to create a mini-cms that allows the admin to create an "html" page with these mysql attributes:
Table Name : Page
-----------------
id :auto-icremented)
name :varchar
In the architecture, if he created the page number "5", the url is
"ww.mywebsite.com/index.php?id=5".
But, this isn't very esthectic so, as I'm very bad at url rewriting even if i read many tutorials, i want to type the name+"html" to access to the page.
If we take the example of the
"www.mywebsite.com/index.php?id=5"
if the admin created a page with the following values:
id : 5
name : 'home'
i want that the user can type
"www.mywebsite.com/home.html"
and with no redirection as i want that this last url must still appear and become the official url.
Thanks for your answer,
i know how to rewrite www.mywebsite.com/index.php?id=5 to www.mywebsite.com/5.html ... but the problem is that i want, first, to get the "name" vale before and in my example, the name value is "home" (5 =>'home').
How can i access to my database with the url rewriting engine?
Thank you very much,
regards.
Use the .htaccess file from a standard Wordpress install to redirect everything to one PHP file. Something like this...
<IfModule mod_rewrite.c>
RewriteEngine On
# Base is the URL path of the home directory
RewriteBase /
RewriteRule ^$ /index.php [L]
# Skip real files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
</IfModule>
Then use $_SERVER['REQUEST_URI'] to figure out what the user was looking for. Modify the .* if you want to make the rule more specific, like .*\.html.
Zakaria,
After using Renesis's rewrite rule, $_SERVER['REQUEST_URI'] will be equal to 'home.html'.
Try something like:
<?php
// clean
$page = mysql_real_escape_string($_SERVER['REQUEST_URI']);
// make query
$query = sprintf("select Page.* from Page where name = '%s'", $page);
// find page ID
if($result = mysql_query($query)){
$page = mysql_fetch_assoc($result);
echo "<pre>", print_r($page, true), "</pre>";
}
?>
Possible output
Array (
[id] => 5
[name] => 'home.html'
)
Use Apache URL Rewriting for this. there are many many examples on this site alone of this. You could also try the official Apache rewriting docs.
You will need to make sure your database enforces uniqueness on name, or you will have problems.
Edit
Have your index.php take a name= parameter instead of a id parameter. You will need to make sure your db has the name field indexed so you don't do a table scan for every page request.

Categories