Jump to content

Recommended Posts

Hey guys,

 

I'm trying to setup a variable for my url so i don't have to type the absolute path to my files in the header or anywhere else or re-type them when loading site to new domain.

 

what i have so far

 

 

var $myurl = 'http://www.domainname.com';

 

any assistance on implementing this and coding it properly will be appreciated

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/
Share on other sites

No need. Check this out

 

If two files a.php and b.php are in the same folder, you can have

 

<?php
header('Location: b.php');
?>

 

Will still send him to b.php, no matter what URL it is. Basically, PHP uses relative paths just as well as absolute paths (Well, it might make a 0.01 second execution time difference)

I think he means like a defined variable usable site-wide. Something like this:

 

includes/define.php:

<?php

$base = 'http://www.domainname.com/';

?>

 

index.php:

<?php

include("includes/base.php"); // I always put includes in a directory. You don't have to.
echo $base;

?>

No need. Check this out

 

If two files a.php and b.php are in the same folder, you can have

 

<?php
header('Location: b.php');
?>

 

Will still send him to b.php, no matter what URL it is. Basically, PHP uses relative paths just as well as absolute paths (Well, it might make a 0.01 second execution time difference)

 

That's technically invalid for a header, but only because of HTTP specs. From the manual: header

Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:
<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>

I think he means like a defined variable usable site-wide. Something like this:

 

includes/define.php:

<?php

$base = 'http://www.domainname.com/';

?>

 

index.php:

<?php

include("includes/base.php"); // I always put includes in a directory. You don't have to.
echo $base;

?>

 

But why? I've never seen an abolute code that couldn't be made relative

nice, quick responses - i hope to learn a lot from this forum.

 

is it a bad form to put

 

<?php

 

$base = 'http://www.domainname.com/';

 

?>

 

<link href="<?php '$base' ?>/styles.css" rel="stylesheet" type="text/css" />

 

is that how i would code it?

 

i'm trying to make my web sites a bit more dynamic and a lot easier to edit after it's developed

 

recommendations on books to?

 

thanks guys

the reason for this would be that my header.php file is an include and i have many pages that are in sub folders so instead of dropping in a the stylesheet or an other javascript file into that folder everytime, i could have $base_url/stylesheet.css etc.. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.