kade119 Posted September 17, 2009 Share Posted September 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/ Share on other sites More sharing options...
Garethp Posted September 17, 2009 Share Posted September 17, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920177 Share on other sites More sharing options...
mattal999 Posted September 17, 2009 Share Posted September 17, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920181 Share on other sites More sharing options...
Philip Posted September 17, 2009 Share Posted September 17, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920183 Share on other sites More sharing options...
Garethp Posted September 17, 2009 Share Posted September 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920184 Share on other sites More sharing options...
kade119 Posted September 17, 2009 Author Share Posted September 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920186 Share on other sites More sharing options...
Philip Posted September 17, 2009 Share Posted September 17, 2009 Just do: <link href="styles.css" rel="stylesheet" type="text/css" /> Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920205 Share on other sites More sharing options...
kade119 Posted September 17, 2009 Author Share Posted September 17, 2009 okay.. i think i'm confusing myself any recommendations on a book that teaches how to build dynamic web sites? Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920207 Share on other sites More sharing options...
kade119 Posted September 17, 2009 Author Share Posted September 17, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920215 Share on other sites More sharing options...
Philip Posted September 17, 2009 Share Posted September 17, 2009 Then make it dynamic by using $_SERVER['HTTP_HOST'] Try running this simple script: <?php echo 'My domain is: '.$_SERVER['HTTP_HOST']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/174601-beginner-setting-up-predefined-var/#findComment-920244 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.