How do i get the id of the user from seo friendly url?
Here's the url (1 is the id)
http://www.website.com/users/edit/1
And, if the url is http://www.website.com/users/edit/1/stack-overflow/ in this case how do i find the id?
I am trying to get it as <?php user = User::find($_GET['id']); ?> but id doesn't work.
<?php
$url = explode("/",($_SERVER["REQUEST_URI"]));
print_r($url);
?>
In your case $url[2] should have the ID that you are after...
The best way to accomplish this is using .htaccess and mod_rewrite to assign the $_GET variables:
.htaccess:
RewriteBase /
RewriteRule ^users/edit/(.*)/(.*)$ users/edit/?user_id=$1&somethingelse=$2 [L,NC]
Then in your php script:
$user_id = $_GET['user_id'];
$somethingelse = $_GET['somethingelse'];
Uhm... I think you should tell us more about the class or framework you are using... But whatever you are doing can't work at all. Because $_GET would only contain something if you would actually pass parameters in the ?id=123 way in the url... So this find will search for nothing because $_GET is empty.
Parse the super global $_SERVER['REQUEST_URI'], which will contain something like users/edit/1/stack-overflow/, with regex or substr and strpos
Related
I am trying to get the value after the / in a URL in PHP.
I have tried using $_GET['va'], but this only works for the following,
http://localhost:8080/default?va=xxx
What I'm trying to get is this,
http://localhost:8080/default/xxx
How do I get the xxx value after a / in PHP.
Any help is greatly appreciated.
Edit
Thanks to everyone who answered, I wasn't very clear in stating what I wanted. It appears what I was looking for is known as a pretty URL or a clean URL.
I solved this by following Pedro Amaral Couto's answer.
Here is what I did;
I modified my .htaccess file on my Apache server, and added the following code.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ default.php?page=$1
RewriteRule ^([a-zA-Z0-9]+)/$ default.php?page=$1
Then I modified my default.php file to GET ['page']
<?php
echo $_GET['page'];
?>
And it returned the value after the "/".
You want to make what is called "pretty URLs" (and other names).
You need to configure the HTTP server appropriately, otherwise you'll get a 404 error. If you're using Apache, that means you may configure .htaccess with RewriteEngine module activated. You also need to add regular expressions.
There's already a question in StackOverflow concerning that subject:
Pretty URLs with .htaccess
Here are another relevant articles that may help:
http://www.desiquintans.com/cleanurls
https://medium.com/#ArthurFinkler/friendly-urls-for-static-php-files-using-htaccess-3264e7622373
You can see how it's done in Wordpress:
https://codex.wordpress.org/Using_Permalinks#Where.27s_my_.htaccess_file.3F
If you follow those, you won't need to change the PHP code, you may use $_GET to retrieve "xxx".
You are looking for: $_SERVER['REQUEST_URI']
The URI which was given in order to access this page; for instance, '/index.html'.
basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
So the $_GET global variable is for parsing the query string.
What you're looking for is the $_SERVER['REQUEST_URI'] global variable:
$url = $_SERVER['REQUEST_URI'];
$url will now contain the full URL of your path. You'll need to use explode('/', $url) to break up that full URL into an array of little strings and parse it from there.
Example:
$pieces = explode('/', $url);
// this will get you the first string value after / in your URL
echo $pieces[0];
You can do in 2 ways.
1- do these steps
Get URL
Explode by /
Get Last element of array
2- Add .htaccess and map that value for some variable
RewriteRule ^default/(.*) page.php?variable=$1
and you can get $_GET['variable']
Clean url in php using htaccess
Here is an example
http://www.example.com/Mobiles/index.php?idd=4
and i want result like this
http://www.example.com/Mobiles
Please help
This is called URL Rewrite. If your page links are dynamic, like extracting data from database which is mostly the case in e-commerce sites, then the best approach is to append the id at the end of URL. This way you can fetch the data from database. Like in your case, your new URL might look like:
http://www.example.com/Mobiles/4
When user will visit this link .htaccess file will internally rewrite this URL to:
http://www.example.com/Mobiles/index.php?id=4
In this way you can then retrieve id from your PHP like this:
$id = $_GET['id'];
or:
extract($_GET);
This extract function will create the variables automatically from the parameters name and you can access it directly with $id variable.
Here is the .htaccess code:
RewriteEngine On
RewriteRule ^Mobiles/(\d+)$ http://www.example.com/Mobiles/index.php?id=$1
In case if you don't need URL like http://www.example.com/Mobiles/4, then use this:
RewriteEngine On
RewriteRule ^Mobiles$ http://www.example.com/Mobiles/index.php?id=4
As I know the only way to get a value from the url is to use the $_GET[]; . To do this the url must be of the format: http://domain.com/index.php?var1=value1
And then to do the following:
<?php
$value1 = $_GET['var1'];
echo($value1);
?>
What I really want is to get the value of the url in the format:
http://domain.com/value1
I know that is not a really php job, but while my index is index.php can I get the value1 from the url? Using something different rather than HTTP Get method.
Cheers.
[SOLUTION]:
I use the URL rewriting to make it work as #KevBot suggest. So what I did:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?value=$1 [L]
It's called URL rewriting. You need to modify .htaccess.
Here's a great tutorial:
I have a data driven site that passes information to determine what the next page should show using the $_GET parameter.
I want the URL's to look nicer and be structured simply.
I have been reading about mod_rewrite but so far failed to implement it.
<?php $post = $_GET['ID']; ?>
<?php $loca = $_GET['loca']; ?>
This is taken from the URL to work out what table we want and what post ID. The URL at the moment is index.php?ID=4&loca=Pages
How would I make this work if it were instead. /pages/(the name column of the post of this ID).
This should do the internal rewrite:
RewriteEngine On
RewriteRule ^pages/(\d+)/ /index.php?ID=$1&loca=pages
It rewrites any url starting with pages/(some number)/ to the result. You should probably add some server side logic as well to do a 302 redirect if the url isn't exactly /pages/id/(Name that matches id)/. You can use $_SERVER['REQUEST_URI'] to get the string and then compare it to the string that it should be and do a redirect if it doesn't match.
Just like if you go to: https://stackoverflow.com/questions/11057691/This+is+not+the+title
You get redirected to the version with the correct title. You should also update the links you have around your site to use the new url format.
There are a lot of examples of how to do this on google.
Tutorial: http://wettone.com/code/clean-urls
Mod_rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
I have searched left and right.
And I am trying to find a script or a method on how I can store links in database and use them for redirection.
This is an E-commerce project and I am looking for something to manage product and system URL from database.
Something similar like magento does.
Basically anything after the domain say like domain.com/demo/12/my_iphone
so now demo/12/my_iphone will be sent to database for query and in databases there will be a destination URL which could be productlisting.php?searchstring=iphones
The user will see link domain.com/demo/12/my_iphone but actually he will be seing productlisting.php?searchstring=iphones
Basically demo/12/my_iphone = productlisting.php?searchstring=iphones
And tomorrow if the user wants to edit demo/12/my_iphone to demo/12/myiphone he can just do so using simple form which will update in the database.
How can I achieve this?
Use mod_rewrite in apache in combination with a PHP script.
1) Make a .htaccess file and put it in a folder, say www.yourdomain.com/a/.htaccess
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteRule .* index.php?%{QUERY_STRING} [L]
2) Create an index.php that handles all requests
(psaudo-code)
<?php
/**
* This script is simply parsing the url used, /demo/12/my_iphone
*/
require_once('library.php');
$request_uri = $_SERVER['REQUEST_URI'];
// parse $request_uri by splitting on slashes /
$params = my_parse_function($request_uri); // <= parse_url() is helpful here!
$param1 = $params[0]; // "demo"
$param2 = $params[1]; // "12";
$param3 = $params[2]; // "my_iphone";
// db-lookup based on the request
$url = fetchUrlFromDB($param1, $param2, $param3);
if($url){
header('Location: '.$url); // $url = "productlisting.php?searchstring=iphones"
die();
}
else{
echo "invalid parameters";
}
?>
It wouldn't be difficult to program such a solution if it comes to that. You could grab the uri from $_SERVER['REQUEST_URI'] and pass it to parse_url to grab the path. Then use the path to query against the database.