URL Friendly Username in PHP?
function Slug($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
$user = 'Alix Axel';
echo Slug($user); // alix-axel
$user = 'Álix Ãxel';
echo Slug($user); // alix-axel
$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel
create user friendly url containing username in php
Try this code in /.htaccess
:
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ myProfile/index.php?u=$1 [L,QSA]
If you still get 404
then verify whether your .htaccess
is enabled or not, by putting same garbage (random) text on top of your /.htaccess
and see if it generates 500 (internal server) error or not when you visit www.mysite.com/index.php?u=username
URL in browser.
url-friendly username function, that checks in db, if it is unique
public function url_username($username)
{
$url_username = str_replace(array('æ','ø','å'),array('ae','oe','aa'),strtolower($username));
$this->db->select('url_username');
$this->db->from('users');
$this->db->like('url_username', $url_username, 'after');
$this->db->order_by('url_username', 'desc');
$this->db->limit(1);
$q = $this->db->get();
if($q->num_rows() > 0)
{
// increment the username with the String Helper
$this->load->helper('string');
$url_username = increment_string($q->url_username, '_');
}
return $url_username;
}
OK, so we just change the query to look for anything that starts with the sanitized url_username
. We use the like
statement for that, then we order by the url_username
descending (meaning myusername_3
is before myusername_2
in the result set). Then we just return the first row (because it will be the largest incremented value) -- we then just increment the end with the String Helper.
Turning a name into a URL-friendly string in PHP?
$s = ' Zhongxiao Dunhua Sun ';
$r = preg_replace('/\W+/', '-', strtolower(trim($s)));
echo("$r\n");
- Replaces non-word characters with dashes
- Converts to lowercase
friendly urls for users profile?
Instead of doing $_GET['userid']
and looking for ID... switch it by looking for name so it's like: viewprofile.php?userid=Ninja123
Try this Mod Rewrite generator: http://www.generateit.net/mod-rewrite/
Creating user friendly URL with directory
Have your rules like this in /profile/.htaccess
:
RewriteEngine On
RewriteBase /profiles/
RewriteCond %{THE_REQUEST} /\?username=([^&\s]+) [NC]
RewriteRule ^ %1? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?username=$1 [L,QSA]
- Your rule was only matching
[0-9]+
after/profile/
, which won't matchsam
. - For adding
.php
you should check it's existence first.
Add 'username' to URL from 'user_id' with PHP for Dummies
This line is wrong according to your information:
RewriteRule ^user/([^/]+)$ index.php?user_id=$1
It should at least be:
RewriteRule ^user/([^/]+)$ /user/index.php?user_id=$1 // for a url like domain/user/username
or using your description:
RewriteRule ^([^/]+)$ /user/index.php?user_id=$1 // for a url like domain/username
Also, you are not setting any variable with the name of username
in the code you have shown, so the check for $_GET['username']
is unnecessary.
Your check in php should look something like:
if ( isset($_GET['user_id']) && queryUserId($_GET['user_id']) ) {
Apart from that you should not use the deprecated mysql_*
functions and use prepared statements as you have an sql injection problem now.
Also note that using two return
statements after each other only returns the first value.
Edit: There seems to be some confusion between the user ID and the username. If the value in the url is a username, you'd better call it username
in both the .htaccess file and php to avoid confusion with the user ID:
RewriteRule ^([^/]+)$ /user/index.php?username=$1 // for a url like domain/username}
and
if ( isset($_GET['username']) && queryUserName($_GET['username']) ) {
in the function (using the deprecated functions just to illustrate...):
function queryUserName($username) {
$conn = dbConnect('read');
$sql = "SELECT * FROM users WHERE username = '".$username."'";
...
}
Rewrite profile URL with username
I want to redirected to login page or
echo
message user does not
exist, but can't figure out how to do that.
Since the action depends on the factor that the user does not exist; you'll need to perform the redirection after you've tried to fetch data for the provided $username
.
Related Topics
How to Find the Mime Type of a File With PHP
Insert Blobs in MySQL Databases With PHP
PHP Regex to Match Outside of HTML Tags
PHP Mail Not Working For Some Reason
How to Use a JavaScript Variable as a PHP Variable
Cannot Use Object of Type Stdclass as Array
Generating a Random Password in PHP
Remove All Attributes from HTML Tags
Create Array Tree from Array List
Select Count(*) from Table of MySQL in PHP
Pdo MySQL: How to Know If Insert Was Successful
How to Embed PHP Code in JavaScript
How to Create a HTML Table from a PHP Array