memerson Posted May 1, 2008 Share Posted May 1, 2008 Hi everyone. I am working on a site (or have been) which includes a file that allows people to enter a query string at the end of the URL stating a code which will dynamically change contact details dependent on who is looking at the site. I include a file called "agent.php" into every page like this: <?php session_start(); header("Content-type: text/html;charset=UTF-8"); require_once("TEMPLATE.php"); require_once("CONFIG.php"); require_once("agent.php"); The template and config files are standards for my websites. The CONFIG file determines which contact details to use - if no query string is specified, then the default values are used. The Config file includes various lines - here is one that determines the phone number displayed on the site: <?php $config["_Site_Telephone"] = $_SESSION["_AGENT_CONFIG"]["_Site_Telephone_Agent"]; if($_SESSION["_AGENT_CONFIG"]["_Site_Telephone_Agent"] == "") { $config["_Site_Telephone"] = "01234567890"; // test phone number for forum purposes!! } The problem however is that it only actions the query upon SECOND loading of the site. That is to say, I need to refresh the page twice in order to make it recognise - which is not much use as I do not want certain people to see certain contact details. The "agent.php" file looks something like this: <?php session_start(); require_once("CONFIG.php"); if(isset($_GET["_"])){ // This is the query string - it's set by placing a "?_=xxx" at the end of the address where xxx is the agent code. $id_agent = $_GET["_"]; $_SESSION["agent_id"] = $id_agent; $agent_request=_makeRequest($api,$user,$check,"get_contact_details","<agent-ref>".$id_agent."</agent-ref>"); // gets the right API if ($agent_xml= _check_request($agent_request)){ $_SESSION["_AGENT_CONFIG"]["_Site_Mode"]="agent"; $_SESSION["_AGENT_CONFIG"]["_Site_Company_Ref"] = (string) $agent_xml->contact[0]->attributes()->ref; $_SESSION["_AGENT_CONFIG"]["_Site_Company_Agent"] = (string) $agent_xml->contact[0]->attributes()->company; $_SESSION["_AGENT_CONFIG"]["_Site_Website_Agent"] = (string) $agent_xml->contact[0]->attributes()->website; $_SESSION["_AGENT_CONFIG"]["_Site_Telephone_Agent"] = (string) $agent_xml->contact[0]->attributes()->tel; $_SESSION["_AGENT_CONFIG"]["_Site_Fax_Agent"] = (string) $agent_xml->contact[0]->attributes()->fax; $_SESSION["_AGENT_CONFIG"]["_Site_Email_Agent"] = (string) $agent_xml->contact[0]->attributes()->email; $_SESSION["_AGENT_CONFIG"]["_Site_Address_Agent"] = (string) str_replace("\n","<br>",$agent_xml->contact->address); $_SESSION["_AGENT_CONFIG"]["_Site_Logo_Agent"] = (string) $agent_xml->contact->logo; $_SESSION["_AGENT_CONFIG"]["_Site_Office_Hours_Agent"] = ""; } } if(isset($_GET["reset"])){ unset($_SESSION["_AGENT_CONFIG"]); unset($_SESSION["agent_id"]); } ///////////////////////////////////////////////// function _makeRequest($api,$user,$check,$type,$params){ $config = $_SESSION["_CONFIG"]; if ($api==""){$api=$config["_Agent_API"]; } if ($user==""){$user= $config["_Agent_Email"];} if ($check==""){$check= $config["_Agent_CheckCode"];} $str ="XML_REQUEST="; $str.="<request>"; $str.="<_debug>1</_debug>"; $str.="<authorise><email>".$user."</email><checkcode>".$check."</checkcode></authorise>"; $str.="<type>".$type."</type>"; $str.="<parameters>".$params."</parameters>"; $str.="</request>"; return _cURL($api,$str); } /////////////////////////////////////////////////////////////// function _cURL($link,$poststring){ $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $link); curl_setopt ($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststring); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ch, CURLOPT_TIMEOUT, 15); $response = trim(curl_exec ($ch)); return $response; } /////////////////////////////////////////////////////////////// function _check_request($xml_request){ if (!$xml = simplexml_load_string($xml_request)) { return false; }else{ //print_r( $xml); if ($xml->status[0]->code!="-1"){ return false; } return $xml; } } /////////////////////////////////////////////////////////////// ?> I hope it's not too confusing, but I really need help on this one and I'm not sure what is causing this problem. Any suggestions and advice would be greatly appreciated. If you need more information, let me know and I'll edit this post with it. Thank you Michael Link to comment https://forums.phpfreaks.com/topic/103690-problem-with-query-string/ Share on other sites More sharing options...
trq Posted May 1, 2008 Share Posted May 1, 2008 You'll need to include agent.php prior to TEMPLATE.php by the looks of it. Link to comment https://forums.phpfreaks.com/topic/103690-problem-with-query-string/#findComment-530892 Share on other sites More sharing options...
memerson Posted May 1, 2008 Author Share Posted May 1, 2008 You'll need to include agent.php prior to TEMPLATE.php by the looks of it. That still doesn't seem to work. I have a feeling it's something to do with the sessions - like it's not creating it first time around, but I don't understand why. Link to comment https://forums.phpfreaks.com/topic/103690-problem-with-query-string/#findComment-530896 Share on other sites More sharing options...
phorman Posted May 1, 2008 Share Posted May 1, 2008 Per the php.net documentation on the function, you should only call session_start once, as it will wipe out querystrings issued between when it was first called and its second call. I noticed that in your main progam you start the session, and then you start the session again in agent.php, which is included in your main program. See about removing the session_start from agent.php and see if that helps. Link to comment https://forums.phpfreaks.com/topic/103690-problem-with-query-string/#findComment-530901 Share on other sites More sharing options...
memerson Posted May 1, 2008 Author Share Posted May 1, 2008 Damn ~ still doesn't seem to work first time around. I think it may be a session problem though, but it doesn't seem to be the session_start. I've removed it anyway. Per the php.net documentation on the function, you should only call session_start once, as it will wipe out querystrings issued between when it was first called and its second call. I noticed that in your main progam you start the session, and then you start the session again in agent.php, which is included in your main program. See about removing the session_start from agent.php and see if that helps. Link to comment https://forums.phpfreaks.com/topic/103690-problem-with-query-string/#findComment-530911 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.