adiadi Posted August 22, 2022 Share Posted August 22, 2022 hi i have piece of php code that i need help to modify, its a http header status control for download page // Stream the file to the end user. header("Content-Type: $mediaType"); header('HTTP2-Settings: HTTP2-Settings: xxxxxxx '); header('Content-Length: ' . filesize($filePath)); //header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . "; filename=\"$fileName\""); i need to add header link rel canonical for current url (dynamic download page url) // Stream the file to the end user. header("Content-Type: $mediaType"); header('link: < https://example.com<?php echo ($_SERVER['REQUEST_URI']); ?>>; rel="canonical"'); header('HTTP2-Settings: HTTP2-Settings: xxxxxxx '); header('Content-Length: ' . filesize($filePath)); //header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . "; filename=\"$fileName\""); but it doesnt work, it gave me Parse error: syntax error, unexpected 'REQUEST_URI' (T_STRING) in xxx on line 260 can someone help me? thank you Quote Link to comment https://forums.phpfreaks.com/topic/315218-get-current-url/ Share on other sites More sharing options...
maxxd Posted August 22, 2022 Share Posted August 22, 2022 You're already in php, so you don't need the `<?php echo` and `?>` around the $_SERVER['REQUEST_URI']. You will, however, have to concatenate as you're using single quotes for that header string and single quotes won't interpolate php code. I'm not overly familiar with the `link` header so I can't say for certain, but I wouldn't be surprised if there were additional syntax errors on that line. Quote Link to comment https://forums.phpfreaks.com/topic/315218-get-current-url/#findComment-1599664 Share on other sites More sharing options...
adiadi Posted August 22, 2022 Author Share Posted August 22, 2022 13 minutes ago, maxxd said: You're already in php, so you don't need the `<?php echo` and `?>` around the $_SERVER['REQUEST_URI']. You will, however, have to concatenate as you're using single quotes for that header string and single quotes won't interpolate php code. I'm not overly familiar with the `link` header so I can't say for certain, but I wouldn't be surprised if there were additional syntax errors on that line. the link header output was suppose to be printed like this Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15 X-Powered-By: PHP/5.5.15 Link: < https://forums.phpfreaks.com/topic/315218-get-current-url/ >; rel="canonical" HTTP2-Settings: HTTP2-Settings: xxxxxxx Content-Length: 201077 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: application/pdf Quote Link to comment https://forums.phpfreaks.com/topic/315218-get-current-url/#findComment-1599665 Share on other sites More sharing options...
Solution maxxd Posted August 22, 2022 Solution Share Posted August 22, 2022 Again - you're already in php mode, so using the opening and closing php tags is redundant and will throw an error. Change this line: header('link: < https://example.com<?php echo ($_SERVER['REQUEST_URI']); ?>>; rel="canonical"'); to this: header('link: <https://example.com/'.$_SERVER['REQUEST_URI'].'>; rel="canonical"'); and see what that gives you. Quote Link to comment https://forums.phpfreaks.com/topic/315218-get-current-url/#findComment-1599667 Share on other sites More sharing options...
requinix Posted August 22, 2022 Share Posted August 22, 2022 If you're specifying a canonical URL then using the REQUEST_URI is almost definitely wrong. Because that will result in your site claiming that the canonical URL is whatever I put into the address bar of my browser. You need to decide what that URL is supposed to be, with correct values (eg, slugs) and without any extraneous query string parameters, and provide that for the Link. Quote Link to comment https://forums.phpfreaks.com/topic/315218-get-current-url/#findComment-1599668 Share on other sites More sharing options...
adiadi Posted August 22, 2022 Author Share Posted August 22, 2022 1 hour ago, maxxd said: Again - you're already in php mode, so using the opening and closing php tags is redundant and will throw an error. Change this line: header('link: < https://example.com<?php echo ($_SERVER['REQUEST_URI']); ?>>; rel="canonical"'); to this: header('link: <https://example.com/'.$_SERVER['REQUEST_URI'].'>; rel="canonical"'); and see what that gives you. yes i understand its already on php ...im just trying to explain what im trying to do (http header output )..im totally blind with php , obviously the syntax are not formatted correctly but it seems the modification isn't that heavy so im trying to figure it out by my own....and thank you this one actually does the job header('link: <https://example.com/'.$_SERVER['REQUEST_URI'].'>; rel="canonical"'); Quote Link to comment https://forums.phpfreaks.com/topic/315218-get-current-url/#findComment-1599670 Share on other sites More sharing options...
adiadi Posted August 22, 2022 Author Share Posted August 22, 2022 (edited) 36 minutes ago, requinix said: If you're specifying a canonical URL then using the REQUEST_URI is almost definitely wrong. Because that will result in your site claiming that the canonical URL is whatever I put into the address bar of my browser. You need to decide what that URL is supposed to be, with correct values (eg, slugs) and without any extraneous query string parameters, and provide that for the Link. hi requinix yes i understand ..my situation is i have 2 version of site where one (A) is an archive and the other one(B) is live site , what im trying to do is put cross domain canonical on archive site (A) to B ..while on B site the canonical tags is self refencing..and thank to @maxxd ..it seems everything goes as i wanted to ..now i just need to test if there's some error or something like that Edited August 22, 2022 by adiadi Quote Link to comment https://forums.phpfreaks.com/topic/315218-get-current-url/#findComment-1599671 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.