Jump to content

Why Does Query String Not Work With Spaces Or Dashes .. ?


Recommended Posts

Hello:

 

I have a problem that has stumped me.

 

I have this query string and mod rewrite in .htaccess:

sitemap.php

echo "<li class='site-map-ul-li'><a href='myPage.php?id=1&myPageURL=Private-Music-Lessons-Private-Music-Teacher&myLocationContent=$myLocationContent' title='".$myLocationContent."'>".$myLocationContent."</a></li>";

.htaccess

RewriteRule ^([0-9]+).([a-zA-Z0-9\-]+).([a-zA-Z0-9\-]+)\.html$ myPage.php?id=$1&myPageURL=$2myLocationContent=$3 [L]

 

If "$myLocationContent" is one word - "Philadelphia" it works fine

 

But, if it is two or more words with either a space or a dash ("Philadelphia PA" or "Philadelphia-PA")

 

it does not work.

 

Any idea why .. ??

 

I ideally want to write the URL as "Philadelphia-PA.html" and display it on the page as "Philadelphia PA"

Hi:

 

Thanks, good catch and good point but it did not fix it ..

 

Not sure what you mean by "exactly what URL are you trying?" but:

(This is generated after I click the link I posted)

 

Works:

http://www.website.com/myPage.php?id=1&myPageURL=Private-Music-Lessons-Private-Music-Teacher&myLocationContent=Philadelphia

 

Does not work

http://www.website.com/myPage.php?id=1&myPageURL=Private-Music-Lessons-Private-Music-Teacher&myLocationContent=Philadelphia%20PA

 

There is no %20 in the address bar after I click the link, but when I copied and pasted it into this post there is.

It displays like this in the address bar:

http://www.website.com/myPage.php?id=1&myPageURL=Private-Music-Lessons-Private-Music-Teacher&myLocationContent=Philadelphia PA

 

It goes to myPage.php via mod rewrite and picks up the session and displays it if it 1 word, but not more than 1 (it keeps keeps the previous session with 1 word):

<?php

session_start();

if (isset ($_GET['myLocationContent'])) {
    if (!ctype_alnum ($_GET['myLocationContent'])) {
		    $error = 'Not a valid city name';
    } else {
		    $_SESSION['myLocation'] = $_GET['myLocationContent'];
    }
}

if(empty ($_SESSION['myLocation'])){
    $_SESSION['myLocation'] = 'Philadelphia';
}
?>

<html>
<?php echo htmlspecialchars ($_SESSION['myLocation']); ?>
</html>

 

 

Any ideas ..??

Hi again:

 

Yes, I would say it is stopping at the first space. Good point.

 

Exact output code - I assume you mean the sitemap.php page and .htaccess file

(I know this can be streamlined/coded better, but this is my first attempt at doing something like this).

I removed only the HTML/CSS markup

 

sitemap.php:

<?php

session_start();

if (isset ($_GET['myLocationContent'])) {
    if (!ctype_alnum ($_GET['myLocationContent'])) {
		    $error = 'Not a valid city name';
    } else {
		    $_SESSION['myLocation'] = $_GET['myLocationContent'];
    }
}

if(empty ($_SESSION['myLocation'])){
    $_SESSION['myLocation'] = 'Lancaster';
}

include('include/db.php');

$con = mysql_connect($db_host, $db_user, $db_password);

mysql_select_db($db_table);

?>

<!DOCTYPE html>

<html>

</head>

<body>

   <h1 class="mm-h1">Site Map</h1>

   <ul class="site-map-ul">

       <?
       $query = "SELECT id, myPageActive, myPageURL, myButtonTitle, myTitle FROM myPageData WHERE myPageActive='Yes' ORDER BY listorder";
       $result = mysql_query($query);
       $num_rows = mysql_num_rows($result);

       for($j=0;$j<$num_rows;$j++)
       {
       $row = mysql_fetch_array($result);
       $id = $row['id'];
       $myTitle = $row['myTitle'];
       $myButtonTitle = $row['myButtonTitle'];
       $myPageURL = $row['myPageURL'];

       echo "<li class='site-map-ul-li'><a href='$id.$myPageURL.".($_SESSION['myLocation']).".html' title='".$myTitle." ".($_SESSION['myLocation'])."'>".$myButtonTitle." ".($_SESSION['myLocation'])."</a></li>";
       }
       ?>

   </ul>

   <h1 class="mm-h1">Locations</h1>


   <ul class="site-map-ul">

       <?
       $query2 = "SELECT myLocationActive, myLocationContent, l_listorder FROM myLocations WHERE myLocationActive='Yes' ORDER BY l_listorder";
       $result2 = mysql_query($query2);
       $num_rows2 = mysql_num_rows($result2);

       for($j=0;$j<$num_rows2;$j++)
       {
       $row2 = mysql_fetch_array($result2);
       $myLocationContent = $row2['myLocationContent'];    

       echo "<li class='site-map-ul-li'><a href='myPage.php?id=1&myPageURL=Private-Music-Lessons-Private-Music-Teacher&myLocationContent=$myLocationContent' title='".$myLocationContent."'>".$myLocationContent."</a></li>";

       }
       ?>

   </ul>

</body>

</html>

 

.htaccess (full file):

# Mod Rewrite
Options FollowSymLinks
Options -MultiViews
RewriteEngine on

RewriteBase /

# This makes the default page the one with an id of 1
RewriteRule ^$ myPage.php?id=$1&myPageURL=$2&myLocationContent=$3 [L]

RewriteRule ^admin/index-login\.html$ admin/index-login\.php [L]

RewriteRule ^sitemap\.html$ sitemap.php [L]

RewriteRule ^sitemap\.xml$ sitemap-XML.php [L]

RewriteRule ^robots\.txt$ robots.php [L]



RewriteRule ^([0-9]+).([a-zA-Z0-9\-]+).([a-zA-Z0-9\-]+)\.html$ myPage.php?id=$1&myPageURL=$2&myLocationContent=$3 [L]

 

myPage.php:

(HTML/CSS removed)

<?php

session_start();

if (isset ($_GET['myLocationContent'])) {
    if (!ctype_alnum ($_GET['myLocationContent'])) {
		    $error = 'Not a valid city name';
    } else {
		    $_SESSION['myLocation'] = $_GET['myLocationContent'];
    }
}

if(empty ($_SESSION['myLocation'])){
    $_SESSION['myLocation'] = 'Lancaster';
}

include("include/db.php");

$con = mysql_connect($db_host, $db_user, $db_password);

mysql_select_db($db_table);

$id = $_GET['id'];

$myPageURL = "";
$myTitle = "";
$myDesc = "";
$myHeader = "";
$mySubHeader = "";
$myPageContent = "";
$myReadMore = "";
$myPageContentMore = "";
$myBannerContent = "";
$photo = "";

$continue = false;

if(isset($_GET)) {
   if($id!="") {
       $query = "SELECT id, myTitle, myDesc, myHeader, mySubHeader, myPageContent, myReadMore, myPageContentMore, myBannerContent, photo_filename FROM myPageData WHERE id = $id";
       $result = mysql_query($query);
       $num_rows = mysql_num_rows($result);
       if($num_rows>0) {
           $row = mysql_fetch_array($result);
           $continue = true;
           $myTitle = $row['myTitle'];
           $myDesc = $row['myDesc'];
           $myHeader = $row['myHeader'];
           $mySubHeader = $row['mySubHeader'];
           $myPageContent = $row['myPageContent'];
           $myReadMore = $row['myReadMore'];
           $myPageContentMore = $row['myPageContentMore'];
           $myBannerContent = $row['myBannerContent'];
           $photo = $row['photo_filename'];
       }
   }
}

if(!$continue) {
   header("location:1.Private-Music-Lessons-Private-Music-Teacher.".($_SESSION['myLocation']).".html");
}

?>

<html>

<body>

<?php echo htmlspecialchars ($_SESSION['myLocation']); ?>

</body>

</html>

 

It definately writes the session variable properly onto myPage.php if it it just 1 word and if I go back to the sitemap.php it is still there, so I know it is storing; it's just not working with the spaces.

 

This is the last piece of this website so any way you know to fix this would be great!

 

Thanks much for the help.

I GOT IT !!!!!

 

Seems that this part:

...
    if (!ctype_alnum ($_GET['myLocationContent'])) {
		    $error = 'Not a valid city name';
...

 

was causing the error

 

New code:

session_start();

if (isset ($_GET['myLocationContent'])) {
	       $_SESSION['myLocation'] = $_GET['myLocationContent'];
}

if(empty ($_SESSION['myLocation'])){
    $_SESSION['myLocation'] = 'Philadelphia PA 19128';
}

 

Is working!

 

One last part: any idea how I can replace the spaces with dashes in the URL *ONLY* .. so I can write the URL as:

Philadelphia-PA-19128

 

And the text on the page as:

Philadelphia PA 19128

 

??

 

Thanks for staying with me and helping me out!

 

I really appreciate it.

str_replace() would be fastest but there's more to it than just that. (raw)urlencode() on top of that is probably a necessity too.

 

You can also use regular expressions to get more powerful replacing logic - not just spaces. If so, what characters do you want to allow in the URL? Obviously letters, numbers, and hyphens, but are there any others? Apostrophes show up in names occasionally. How about accented characters?

Edited by requinix

Hi there:

 

Just letters, numbers and hyphens.

 

No special characters; just trying to keep it straight-forward.

 

Is this something I would do in the SESSION, and the have 2 variables to use .. ??

 

Or, can I somehow work it into the URL .. ??

header("location:1.Private-Music-Lessons-Private-Music-Teacher.".($_SESSION['myLocation']).".html");

 

 

Thanks!

 

 

EDIT:

 

Thinking about it, if I can just replace the spaces with hyphens in the URL string I posted, I would be more than happy! Possible?

Edited by spacepoet

Trying it this way:

echo "<li class='site-map-ul-li'><a href='1.Private-Music-Lessons-Private-Music-Teacher.".str_replace(" ", "-" $myLocationContent).".html' title='".$myLocationContent."'>".$myLocationContent."</a></li>";

 

but I keep getting errors ..

 

Any idea how to correct this .. ??

 

I'm excited - almost there!

Sigh ....

 

Maybe it would be better to try mod re-write .. ??

 

I have it working on the link I posted:

echo "<li class='site-map-ul-li'><a href='1.Private-Music-Lessons-Private-Music-Teacher.".(str_replace(" ","-","$myLocationContent")).".html' title='".$myLocationContent."'>".$myLocationContent."</a></li>";

 

but I since it is 1 SESSION, it is also writing the dashes into the location on the page ..

 

Can I somehow add this to my .htaccess for the 2 URL re-writes so it only writes it into the URLs .. ??

RewriteRule ^([0-9]+).([a-zA-Z0-9\-]+).([a-zA-Z0-9\-]+)\.html$ myPage.php?id=$1&myPageURL=$2&myLocationContent=$3 [L]

RewriteRule ^1.Private-Music-Lessons-Private-Music-Teacher.([a-zA-Z0-9\-]+)\.html$ myPage.php?id=1&myPageURL=Private-Music-Lessons-Private-Music-Teacher&myLocationContent=$3 [L]

Edited by spacepoet

Or .. is it possible to split the SESSION into 2 parts/names so I can use 1 for the URL and 1 to display on the pages .. ??

session_start();

if (isset ($_GET['myLocationContent'])) {
                          $_SESSION['myLocation'] = $_GET['myLocationContent'];
}

if(empty ($_SESSION['myLocation'])){
               $_SESSION['myLocation'] = 'Philadelphia PA 19128';
}

 

Any ideas on this ...

So it creates a much nicer SEO friendly URL, and it gets listed on the page with the service:

1.Private-Music-Lessons-Private-Music-Teacher.html

 

Becomes

 

1.Private-Music-Lessons-Private-Music-Teacher.Philadelphia.PA.19128.html

 

And gets listed on the page that way.

 

Page:

<html>
Private Music Lessons | Private Music Teacher
</html>

 

Becomes

 

<html>
Private Music Lessons | Private Music Teacher | Philadelphia PA 19128
</html>

 

I did this when I programmed with Classic ASP and it worked like a charm; just need to figure out this last piece.

 

Any ideas .. ??

Edited by spacepoet

ISSUE SOLVED !!!

 

I am glad!

 

I used the $l_id (Locations id) to write the URL and Location as two different pieces of data.

 

Looking back I should have done that in the first place, but this is all part of a learning process.

 

Happy Holidays to me! And everyone else!!

 

:)

Edited by spacepoet
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.