Jump to content

get current url


adiadi
 Share

Go to solution Solved by maxxd,

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"');

 

Link to comment
Share on other sites

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 by adiadi
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.