Browser not accepting "+" in the title in the URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Browser not accepting "+" in the title in the URL

Post by simonmlewis »

If I use [L] rather than [QSA] that still fails.
But as I say, the current site has over 100 products with + in the title and they all work fine. But now... with this new structure, they don't. On the product page, I've not added anything to make it 301.

I've found what is making it do it, but not 'why' it is doing it.

I have this script in an external included file, that takes the page back to a subcategory if the product doesn't exist. And it checks that the product title is correct.

Maybe it's this?? As this is not on the LIVE version. If it is this, how can I get around the problem?

Code: Select all

<?php
  $myprod = isset($_GET['product']) ? $_GET['product'] : null;
  if (isset($myprod))
  {
  // gather $h which has the title in the url, and remove the hyphen.
  $h = isset($_GET['h']) ? $_GET['h'] : null;
  $h = str_replace("-"," ",$h);
  
  $sname = isset($_GET['sname']) ? $_GET['sname'] : null;
  $cname = isset($_GET['cname']) ? $_GET['cname'] : null;
  
  // check if the product ID exists at all
  $queryid = "SELECT id, catid, subid, catname, subname, title FROM products WHERE id = :myprod";
  $stmtid = $pdo->prepare($queryid);
  $stmtid->execute(array(':myprod' => $myprod));
  // if it doesn't exist, 301 to the URLs subcategory page (let the subcategory page handle it beyond that
  if ($stmtid->rowCount() == 0)
    {
    header("Location: /subcateg/$cname/$sname", TRUE, 301);
    exit();
    }
  else
  {
  // if the ID is correct, it then checks if the title is correct.  If it is NOT right, it rewrites it from the database.
  // else it just ignores this whole thing and passes it through
  $query = "SELECT id, catid, subid, catname, subname, title FROM products WHERE title = :h";
        $stmt = $pdo->prepare($query);
        $stmt->execute(array(':h' => $h));
        if ($stmt->rowCount() == 0)
        {
         while ($row = $stmtid->fetch(PDO::FETCH_OBJ)) 
           {
           $title = str_replace(" ","-",$row->title);
            header("Location: /product/$cname/$sname/$row->id/$title", TRUE, 301);
            exit();
           } 
        }
    }
}
?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Browser not accepting "+" in the title in the URL

Post by Celauran »

Code: Select all

  // if the ID is correct, it then checks if the title is correct.  If it is NOT right, it rewrites it from the database.
  // else it just ignores this whole thing and passes it through
  $query = "SELECT id, catid, subid, catname, subname, title FROM products WHERE title = :h";
        $stmt = $pdo->prepare($query);
        $stmt->execute(array(':h' => $h));
        if ($stmt->rowCount() == 0)
        {
         while ($row = $stmtid->fetch(PDO::FETCH_OBJ))
           {
           $title = str_replace(" ","-",$row->title);
            header("Location: /product/$cname/$sname/$row->id/$title", TRUE, 301);
            exit();
           }
        }
    }
Looks to me like this is the problem. + in URI is being interpreted as a space (see my var_dump earlier) so title is "wrong", being rewritten, redirecting the user, and the cycle repeats.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Browser not accepting "+" in the title in the URL

Post by simonmlewis »

.. and no way to say if it does find a +, leave it there? We have products called something like:

Product 1+
Product 1++
Product 1

If they remove all thsoe plusses, they are a bit knackered.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Browser not accepting "+" in the title in the URL

Post by Weirdan »

Maybe it's this?? As this is not on the LIVE version. If it is this, how can I get around the problem?
Quite likely. Imagine that plus sign gets converted into space somewhere. This script checks the title, finds that it should contain plus and not the space it got, and issues 301 redirect. Browser requests the new url (with plus sign), it gets converted into space and it gets to this script again... ad infinitum. Eventually browser grows tired and complains.

Add

Code: Select all

var_dump($title); die();
, comment out title redirect and post the results.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Browser not accepting "+" in the title in the URL

Post by Celauran »

You could leave them off, urlencode them, come up with separate slugs for products; there are many ways to work around this.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Browser not accepting "+" in the title in the URL

Post by simonmlewis »

IS there a way to urlencode this particular script... rather than me going thru hundreds and hundreds of existing links on the site?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Browser not accepting "+" in the title in the URL

Post by Celauran »

You'd be urlencoding the title, so instead of cat-+-mouse, you'd have cat-%2B-mouse. Not really much better.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Browser not accepting "+" in the title in the URL

Post by Celauran »

You could, however, very easily programmatically generate slugs for all the products.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Browser not accepting "+" in the title in the URL

Post by simonmlewis »

how.....??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Browser not accepting "+" in the title in the URL

Post by Celauran »

Could be something as simple as that. Add conditions as you need. Could even catch the + and replace with 'plus' if you're so inclined.

Code: Select all

function slugify($title)
{
    return str_replace(' ', '-', strtolower($title));
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Browser not accepting "+" in the title in the URL

Post by simonmlewis »

So you are suggesting rather than using

Code: Select all

$title = str_replace(" ","-",$row->title);
To use that instead? Would that solve it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Browser not accepting "+" in the title in the URL

Post by Celauran »

That's not what I'm suggesting, no. I'm suggesting adding a column specifically for slugs. You can keep the title intact, slugify all the product titles, keep bad characters out of slugs, and perform your lookup against slug rather than title.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Browser not accepting "+" in the title in the URL

Post by simonmlewis »

Good heavens I've no idea how to do that.
All our links are just rewritten to add the - for a space. Then the page loads.
So not sure how I would do that, without going thru every single page.

All because of this little script I posted. Pity that script cannot be altered to allow the + through as a +. Since it is in there where it dislikes it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Browser not accepting "+" in the title in the URL

Post by simonmlewis »

I guess what is happening is without that script running, it just passes it through.
But when it does run, it see the - as a -, and the + as a space.
So maybe, I could at the top do something like this:

Code: Select all

$h = isset($_GET['h']) ? $_GET['h'] : null;
  $h = str_replace(" ","+",$h);
  $h = str_replace("-"," ",$h);
Might not work, and I will test it after submitting this... but in theory, a + is the only thing causing a space.... so I could put it back?!

edit: it works!
Any flaws?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Browser not accepting "+" in the title in the URL

Post by Celauran »

simonmlewis wrote:Good heavens I've no idea how to do that.
Sure you do.

Add the slug column to your products table.
Query all products.
Loop through them, passing the title to some slugify function and store its return value.
Update products.

Once that's done, you just modify your query that currently checks for title to check for slug instead.
Post Reply