TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-23-2011, 11:38 AM   #1 (permalink)
The Visitor
 
Join Date: Jun 2011
Posts: 3
Thanks: 0
p1nky23 is on a distinguished road
Default How do I remove the article ID from the URL?

I've managed to set up my links so that they run to the url I want. Originally they were running to 'myweb.com/blog/1/title' but i've decided that's not quite good enough and want to remove the article id from the url so it reads 'myweb.com/blog/title'. I understand I need to get the blog reading from the $title row and not the $id but I'm not quite sure how to do this with my code, which is this:

PHP Code:
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}

$id = (int)$_GET['id'];
$sql = "SELECT * FROM php_blog WHERE id='$id' LIMIT 1";

$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());

while($row = mysql_fetch_array($result)) {

$date = date("l F d Y", $row['timestamp']);

$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
$password = $row['password'];
$get_categories = mysql_query("SELECT * FROM php_blog_categories WHERE `category_id` = $row[category]");
$category = mysql_fetch_array($get_categories);

if ($password == 1) {
if (isset($_POST['username']) && $_POST['username'] == $my_username) {
if (isset($_POST['pass']) && $_POST['pass'] == $my_password) {
?>
<p><strong><?php echo $title?></strong><br /><br />
<?php echo $entry?><br /><br />
Posted in <?php echo $category['category_name']; ?> on <?php echo $date?></p>

<?php
}
else { 
?>
<p>Sorry, wrong password.</p>

<?php
}
}
else {
echo 
"<p><strong>" $title "</strong></p>";

printf("<p>This is a password protected entry. If you have a password, log in below.</p>");

printf("<form method=\"post\" action=\"blog_single.php?id=%s\"><p><strong><label for=\"username\">Username:</label></strong><br /><input type=\"text\" name=\"username\" id=\"username\" /></p><p><strong><label for=\"pass\">Password:</label></strong><br /><input type=\"password\" name=\"pass\" id=\"pass\" /></p><p><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"submit\" /></p></form>",$id);
print 
"<br /><br />";
}
}
else { 
?>
I'm open to suggestion on ways to do this btw, having the blog call the title instead of the id is the only way I've read to do it so far.

At the moment, to achieve 'myweb.com/blog/article-title/' i have this in my .htaccess

Quote:
RewriteRule ^blog/([^/\.]+)/?$ blog_single.php?title=$1 [L]
This at the moment however doesn't allow the page to call from the
database
as the code is looking for the ID.


When i had 'myweb.com/blog/1/article-title' I had this is my .htaccess:

Quote:
RewriteRule ^blog/([^/\.]+)/?/([^/\.]+)/?$ blog_single.php?id=$1&title=$2 [L]
And that called from the database fine.
------------------
Any help would be gratefully appreciated, thank you in advance.

Last edited by codefreek : 06-30-2011 at 07:07 PM. Reason: PHP Tags.
p1nky23 is offline  
Reply With Quote
Old 06-26-2011, 06:05 PM   #2 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

your query needs to change:

Code:
if (!isset($_GET['title']) || !is_string($_GET['title'])) {
die("Missing title");
}

$title = (string)mysql_real_escape_string($_GET['title']);
$sql = "SELECT * FROM php_blog WHERE title='{$title}' LIMIT 1";

$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());

while($row = mysql_fetch_array($result)) {
 // continue on with the rest of your code...
__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck is offline  
Reply With Quote
Old 08-22-2011, 09:55 AM   #3 (permalink)
The Visitor
 
Join Date: Jun 2011
Posts: 3
Thanks: 0
p1nky23 is on a distinguished road
Default

Hi, bit of a late reply. Thank you for the suggestion, however the page displays the error "missing title" when it loads, any idea why it would be doing this?
p1nky23 is offline  
Reply With Quote
Old 08-22-2011, 10:22 AM   #4 (permalink)
The Visitor
 
Join Date: Jun 2011
Posts: 3
Thanks: 0
p1nky23 is on a distinguished road
Default

Nevermind, sorted this by creating a separate input for the title url
p1nky23 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove Reset button from whole site dhaval General 3 12-22-2009 05:13 PM
Security Article / Tutorial on PHPFreaks.com drewbee General 0 07-03-2008 04:36 PM
can someone write an article regarding passing variables from one page to another sarmenhb General 3 05-18-2008 12:49 AM
Breaking up an article into pages danielneri General 5 01-04-2008 08:02 PM
Gallery article Tanax Advanced PHP Programming 2 11-30-2007 05:33 PM


All times are GMT. The time now is 07:08 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design