Coding advice request (base url in php)

Hello!

has enyone been able to get the baseurl in php? I want to add my domain name on a link, but I dont want to hardcode the name, I’ve been trying some codes (produced by chatgpt, I’m not an expert) that dont work…

this returns empty

function getBaseUrl() {
    $protocol = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') ? 'https://' : 'http://';
    $host = $_SERVER['HTTP_HOST'];
    $scriptPath = dirname($_SERVER['PHP_SELF']);
    return $protocol . $host . $scriptPath;
}

this returns …/…/

function getBaseUrl() {
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $domainName = $_SERVER['HTTP_HOST'];
    return $protocol . $domainName;
}
?>


Do not rely on ChatGPT if you are unaware of what is going on.

13 Likes

I agree with @Ziverre

Using ChatGPT to generate code, especially in a language you don’t fully understand is the best way in modern development to get security vulnerabilities and bugs (It’s also the best way to get yourself called a few words when asking for help with code that makes no sense). Use sites like StackOverflow, or other online forums that have real users posting code.

14 Likes

This should get the base url

<?
    $yourdomain = $_SERVER['HTTP_HOST'];
    $yourdomain = preg_replace('/^www\./' , '' , $yourdomain);
?>

This should display some page

<head>

	<title><?echo $yourdomain;?></title>

</head>

Just fiddle with it to get what you want

8 Likes

Hey there @_I_F_R :wave:

This should work:

$url = 'http://' . $_SERVER['HTTP_HOST'] . ;

Please replace http with https if using SSL.

Cheers :tada:

6 Likes

thanks for the advice, I used your codes and it worked :smiley:

Regarding the advice to use ChatGPT to code… well, I’m just doing a personal project to learn what it can do, I ask for code, modify it a liottle bit, iterate asking him/her/it … and most of the times I get a solution, I know itnt the safest or the best solution, but a solution nevertheless.
Lets say I’m coding a MVP, if it convices my boss, then a software company will code it professionally

thanks!

4 Likes

stackoverflow is a pain as its virtually impossible to post a question as their automated post screening code will keep complaining

and if you do actually manage to post your question then you’ll be bombarded by a bunch of big headed toxic jerks who wont really give you any help

I recommend posting on https://codeforum.org/

7 Likes

I still prefer to search their topics (google search results does it for me) for how to solve my problem and I will be sure that a real person has put his thoughts and creativity to produce an accurate code unlike GPT. Though I agree that the community is bad.

7 Likes

GPT and other AI can be a useful source of code. However, it is best that you have at the very least an inkling of what you are doing. If not and you decide to use AI to generate code anyway, you may want to (after making sure that it works) make sure that no vulnerabilities or other weaknesses exist in it.

Personally, I do use AI (Codeium AI). Most of my projects I code from scratch, however, and therefore do not run into any security issues (because I am for the most part utilizing basic operations in Javascript). However, I’ve found that it becomes hit-or-miss when it comes to programming in a language like C or utilizing more complex or undocumented features (like custom code for example).

Regardless, be careful when using it. I’ve had AI break my code because it didn’t know what it was doing more than once.

Another suggestion is to use Bing AI or Google Bard (I haven’t heard good results about the last one… at least, when it came out), as they can find good answers to your coding question to (potentially) save some time browsing the internet for resources.

7 Likes

I really appreciate your comments.

Just to clarify, I’m coding (gpt is) a basic website to handle repair requests, I do it as a way to learn in one hand, but lost importantly, to think while doing of what I need from the system and what relationships and automatisms I can have. I iterate many times over the same form, etc … it’s a way of preparing a “guide” for the real coders (if the project is accepted) do exactly what I need.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.