man5 Posted April 17, 2014 Share Posted April 17, 2014 Right now I am using a session to get search's input terms and place them into meta tags(title). It works; the only issue is that the search terms lag 1 page behind. For eg. If i search "flowers" on first try, it will show flowers in the metas(title). Next I input "treehouse" in search; instead of "treehouse" showing in the metas, it'll still show me the previous term "flowers". Now if I do a third search with a new term, it won't show the new term in metas, it'll show me the previous term "treehouse". Why is this happening? Am I doing something wrong? This is only an issue with meta tags. Search itself works fine. It shows results on a page as it's suppose to. Quote Link to comment https://forums.phpfreaks.com/topic/287853-how-do-you-get-search-boxs-input-terms-into-meta-tags/ Share on other sites More sharing options...
mhmazrooei Posted April 17, 2014 Share Posted April 17, 2014 HI. when do you set you session variable ? Can you post your code here ? Quote Link to comment https://forums.phpfreaks.com/topic/287853-how-do-you-get-search-boxs-input-terms-into-meta-tags/#findComment-1476528 Share on other sites More sharing options...
man5 Posted April 17, 2014 Author Share Posted April 17, 2014 HI. when do you set you session variable ? Can you post your code here ? Well my session_start(); is at the very begining of the page. My site setup is done in layers with header,head,footer,foot divded up; but here is a basic layout with my search session. <?php init.php ?> //this includes the sesion_start(); <html> <head> $searchsession = $_SESSION['searchsession']; <title><?php echo $searchsession; ?></title> <meta name="description" content="Search page description here." /> </head> <body> <?php $value = $_GET['search']; $_SESSION['searchsession'] = $value; ?> <form action="search.php?q=<?php echo $value; ?>" method="GET"> <input type="search" name="search" maxlength="60" placeholder="Search..."> <input type="submit" name="submit" value="Search" > </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/287853-how-do-you-get-search-boxs-input-terms-into-meta-tags/#findComment-1476533 Share on other sites More sharing options...
adam_bray Posted April 17, 2014 Share Posted April 17, 2014 (edited) It's simply down to the order you're processing the page - Step 1 - $searchsession = $_SESSION['searchsession']; Let's say $_SESSION['searchsession'] = '1'; This line is setting $searchsession = '1'; Step 2 - <title><?php echo $searchsession; ?></title> You echo $searchsession, so the title will be '1'. Step 3 - $value = $_GET['search']; You assign the variable $value the same value as $_GET['search'].. let's say that value is '2'. Step 4 - $_SESSION['searchsession'] = $value; You change the existing $_SESSION['searchsession'] variable to '2'. Step 5 - <form action="search.php?q=<?php echo $value; ?>" method="GET"> You echo the $value variable in your form, this is '2' as it's come from the $_GET['search'] variable. Look at the order things are happening above. What you're looking for is step 1 - 5 the variable to always be 2. By changing the order of the 5 steps above you'll get what you want. Edited April 17, 2014 by adam_bray Quote Link to comment https://forums.phpfreaks.com/topic/287853-how-do-you-get-search-boxs-input-terms-into-meta-tags/#findComment-1476541 Share on other sites More sharing options...
Solution man5 Posted April 17, 2014 Author Solution Share Posted April 17, 2014 It's simply down to the order you're processing the page - Step 1 - $searchsession = $_SESSION['searchsession']; Let's say $_SESSION['searchsession'] = '1'; This line is setting $searchsession = '1'; Step 2 - <title><?php echo $searchsession; ?></title> You echo $searchsession, so the title will be '1'. Step 3 - $value = $_GET['search']; You assign the variable $value the same value as $_GET['search'].. let's say that value is '2'. Step 4 - $_SESSION['searchsession'] = $value; You change the existing $_SESSION['searchsession'] variable to '2'. Step 5 - <form action="search.php?q=<?php echo $value; ?>" method="GET"> You echo the $value variable in your form, this is '2' as it's come from the $_GET['search'] variable. Look at the order things are happening above. What you're looking for is step 1 - 5 the variable to always be 2. By changing the order of the 5 steps above you'll get what you want. Thanks for pointing me the right direction. I was getting confused sessions and variables because I had divided up the pages/sections; but by playing around with it more, I finally came to a solution. It works now. Quote Link to comment https://forums.phpfreaks.com/topic/287853-how-do-you-get-search-boxs-input-terms-into-meta-tags/#findComment-1476554 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.