abeer Posted June 15, 2011 Share Posted June 15, 2011 Hi there.. Ineed help about dynamic meta. like title,meta description,meta tags please first visit the link its a demo site link and visit pages apple.banans etc.. all the pages loads from databses from getting value from url look the the page title. meta tags are not changed... because i dont know hoe to change them dynamically.. can anyone help this.. 1.How to change meta tags dynamically? any tutorial? 2.is it possible without php.mysql? by xml? please help me Quote Link to comment https://forums.phpfreaks.com/topic/239413-how-to-change-meta/ Share on other sites More sharing options...
Fadion Posted June 15, 2011 Share Posted June 15, 2011 As every page will need to have it's own title and meta tags ("description" I guess), your best bet is adding them to the article rows. In example: Table "articles" -------------------------------------------- id | title | content | meta_description -------------------------------------------- 1 | Apples | Apples are healthy to eat everyday | Not all apples are red <?php $id = $_GET['id']; $results = mysql_query("SELECT title, content, meta_description FROM articles WHERE id=$id"); $values = mysql_fetch_assoc($results); $title = $values['title']; $content = $values['content']; $meta_description = $values['meta_description']; ?> <html> <head> <meta name="description" content="<?php echo $meta_description; ?>" /> <title>MySite.com - <?php echo $title; ?></title> </head> <body> <h1><?php echo $title; ?></h1> <?php echo $content; ?> </body> </html> The code is just to give you a basic idea of how to handle it, but you'll need to validate input, check if the article exists, stripslashes, etc. Quote Link to comment https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1229941 Share on other sites More sharing options...
abeer Posted June 15, 2011 Author Share Posted June 15, 2011 Thanks mate. i got idea from you,reaaly happy... can you please chelc out if iam right or wrong? as i am very new in php please corection me if did anything wrong.. Table name: page ---------------------------------------------------------------------- id | title | meta_keywords | meta_description | dtl(content) ---------------------------------------------------------------------- 1 | apples page |apple,red,sweet,green |apples are green | appleas are green,red,blue bla bla bla bla,this iscontent and whole structure of my page is now <?php mysql_connect("localhost", "ash", "lash") or die("Connection Failed"); mysql_select_db("lash")or die("Connection Failed"); $id = isset($_GET['id']) ? filter_var($_GET['id'], FILTER_VALIDATE_INT) : NULL; if (is_int($id)) { $result = mysql_query("SELECT title, dtl, meta_description,meta_keywords FROM page WHERE id='$id'") or die(mysql_error()); $row = mysql_fetch_array($result); ?> <html> <head> <meta name="description" content="<?php echo $meta_description; ?>" /> <meta name="keywords" content="<?php echo $meta_keywords; ?>" /> <title>MySite.com - <?php echo $title; ?></title> </head> <body> <h1><?php echo $title; ?></h1> <?php echo $dtl; ?></body> </html> Please please correct me if anything wrong Quote Link to comment https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1229990 Share on other sites More sharing options...
Fadion Posted June 15, 2011 Share Posted June 15, 2011 <?php mysql_connect('localhost', 'ash', 'lash') or die('Connection Failed'); mysql_select_db('lash') or die('Database not found'); $id = (int) $_GET['id']; $result = mysql_query("SELECT title, dtl, meta_description, meta_keywords FROM page WHERE id=$id"); if (!mysql_num_rows($result)) { header('Location: 404.php', true, 404); die(); } $row = mysql_fetch_array($result); $title = stripslashes($row['title']); $dtl = stripslashes($row['dtl']); $meta_desc = stripslashes($row['meta_description']); $meta_keys = stripslashes($row['meta_keywords']); ?> <html> <head> <meta name="description" content="<?php echo $meta_description; ?>" /> <meta name="keywords" content="<?php echo $meta_keywords; ?>" /> <title>MySite.com - <?php echo $title; ?></title> </head> <body> <h1><?php echo $title; ?></h1> <?php echo $dtl; ?></body> </html> As the script is intended for production, I added some control if the row exists. You can't show an article when it doesn't exist, right? In that case it does a redirect (with a 404 status code) to a "404.php" page. The best way to do that would be just sending a 404 header and creating an .htaccess file with an ErrorDocument for 404. Anyway, it will work the same. I added the variables too (ie. $title = stripslashes($row['title'])) Quote Link to comment https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1230003 Share on other sites More sharing options...
abeer Posted June 15, 2011 Author Share Posted June 15, 2011 oh great. really great.. is this script risk free from sql injection? Quote Link to comment https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1230011 Share on other sites More sharing options...
Fadion Posted June 15, 2011 Share Posted June 15, 2011 Actually no. Because the 'id' GET variable is converted to integer, it won't break the query. What it may do however, is get 0 results from the query if it is manipulated, something taken care of the: if(!mysql_num_rows($result)) part. You're safe to go. Quote Link to comment https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1230057 Share on other sites More sharing options...
abeer Posted June 15, 2011 Author Share Posted June 15, 2011 Salute to you. i have no words to thank you.... thanks mate very much Quote Link to comment https://forums.phpfreaks.com/topic/239413-how-to-change-meta/#findComment-1230124 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.