Jump to content

$_SERVER[‘HTTP_HOST’] in href?


Recommended Posts



I'm trying to add php code into my URL. I have the following code:

<div class="btn-group" style="padding-left: 20px;" role="group">
    <a href="<?php echo $_SERVER['HTTP_HOST']; ?>/09php/html_forms.php"><button type="button" class="btn btn-default">Go back to the form</button></a>

The link should take me to: http://localhost:8080/09php/html_forms.php


The URL is correct, but the browser cannot access it.


What am I doing wrong?



Link to comment
Share on other sites

The reason it's not working is because you forgot to prepend a protocol to the URL:

http://<?php echo $_SERVER['HTTP_HOST']; ?>/09php/html_forms.php

There are some things that should be fixed:
1. You shouldn't have a button inside a link, just put the class name on the link itself
2. You actually don't need $_SERVER['HTTP_HOST'] because you can make a link relative to the site root just by prepending a slash.

<a href="/09php/html_forms.php" class="btn btn-default">Go back to the form</a>
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...