SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    UK
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a variable constant for url root??

    I have a site which uses relative urls throughout. I am now trying to implement https for just a few pages, and it is turning into a right mess.

    I tried putting the full path (https://... etc) in the links to the few pages that need it, and then realized, once you have visited these pages, of course the other pages keep that same url root.

    So, i thought, i will just set a variable to the correct url root at the top of each page and include that in my links. Problem with that is i use loads of functions that also need to know about it - i really don't want to have to pass it to every one.

    What i want is a superglobal - but apparently there is no such thing as a user-defined one (?) - or a constant which i can change on each page - which apparently you can't do (?).

    The only way i can think of doing it is to use a SESSION var, which is a whole pile of extra coding. I'm sure there must be a better way.

    As well as the http/https choice, i also want to be able to set the url to my localhost, for development.

    Any advice gratefully received......

  2. #2
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't fully understand what you're trying to accomplish, but I imagine define() should give you the per-request, "variable" constant effect you're looking for...

    PHP Code:
    define('URL_ROOT''https://www.example.com');

    // ...

    echo URL_ROOT "/user/display.php"
    ...though I have a feeling there's a more elegant way to handle this problem you're having.
    Drew C King: PHP Developer
    <?= $short_tags++ ?>

  3. #3
    SitePoint Zealot evilunix's Avatar
    Join Date
    Jun 2008
    Location
    York, UK.
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use define as above, but as far as I know you can't put this inside of an if statement.

    Your other options are:

    Passing the variable into your functions as usual.

    or

    global $myvar inside your function.. Quick way of simulating a global variable although programmatically not very correct!

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You could just use "/" prefixing your relative URIs making them closer to absolute. For example => '/store/images/mmm.jpg' is the same as 'http://example.com/store/images/mmm.jpg' at least in the eyes of a browser.

    As for the constant switching between HTTP and HTTP + SSL...
    PHP Code:
    define'BASE_URL''http' . ( $_SERVER['HTTPS'] == 'on' 's' '' ) . '://' $_SERVER['HTTP_HOST'] ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $GLOBALS is superglobal.
    A constant can be changed once per script execution. For you this is probably once per page.

    You could also use either a function with a static variable, or a singleton object to manage the config variables for you.

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    UK
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies.

    cholmon, if it helps you come up with a more elegant solution, i'll try to explain a bit better.

    for my home page i want to display http://www.localfare.co.uk/tpi/home.php, but for the login i want use https://theta.securesslhost.net/~localfa/tpi/login.php . Once the user has been to the login page, if he then selects home it uses https://theta.securesslhost.net/~localfa/tpi/home.php (because of my relative urls...)

    the define sounds like the way to go - i define a URL_ROOT at the top of each page, and any functions called from that page know about it too, correct?

    although, if you're right, evilunix, and you can't put a define in an IF statement, it's not so useful for a quick change to localhost.co.uk for developing. i guess i could put the define into an include file, and put that in an IF statement ?

    i'll give it a go.

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Err...you have a serious error, treacle, in your implementation. The secure URL (HTTPS) changes domains, you really do not want to do that, because your cookies and sessions will fail. It should be https://your-domain-name not your host domain.

    As for developing on localhost, should look into virtual hosting and domains. I personally use http://[client-name].clients.loc, http://[project-name].projects.loc, http://sandbox.loc for all my testing and developing. Doing this would get you out of using sub folders for each site/project and closer to the real thing.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    UK
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for more replies...........

    crmalibu - i think you're talking a little above my head. I don't know a singleton object, nor very much about static variables. But the constant being set each script execution makes sense - it should work on each page.

    logic_earth - yeah, localfare is my test site, and wiserhosting offer a shared ssl service. i was only using it for testing. i will get proper domain & ssl certificates before anything goes live!
    i think i follow what you're saying, with the base_url, but won't https be permanently on, once the user has been to a single secure page, unless at some point i set a new url?
    This is my first site, so i am kind of making it up as i go along. I write everything locally, and ftp it all to a test site (localfare) after each major change. Isn't that how everyone does it???

    Do you actually edit your source remotely?.

  9. #9
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by treacle0996 View Post
    I write everything locally, and ftp it all to a test site (localfare) after each major change. Isn't that how everyone does it???

    Do you actually edit your source remotely?.
    FWIW, my dev environment is set up like so:

    1) my local machine runs windows XP pro

    2) I run a virtualized Ubuntu server via VirtualBox, which is free. It helps to have loads of ram though, like >2gb

    3) my live server is also ubuntu (vps hosted at SliceHost for $20/mo), and it's setup with the exact same LAMP stack as my local vbox.

    4) For each local site i develop, I create a project in svn (the repository resides on the live server) and check it out onto my local dev environment. a "project" is the vhost in its entirety.

    5) I do all my code editing locally in ubuntu, but test in windows. You can set virtualbox to use a virtual network interface so that it can actually serve http on your network as if it were its own machine. And with dual monitors, you can have your vbox/ubuntu on the left monitor for editing code, and the windows browsers (IE, FF, Chrome, Opera, etc) on the right monitor. It's very seamless.

    6) When i'm ready to roll the changes to the live server to do real testing, I just check my local code back into the repository, then SSH to the live server and svn up to pull down that new code and test the app like normal with IE and FF.

    7) similar to logic_earth, I use hostnames (via windows' HOSTS file) like http://example.local/ for local testing, then http://dev.example.com for live testing.
    Drew C King: PHP Developer
    <?= $short_tags++ ?>

  10. #10
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    UK
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, i have so much to learn.......

    my set up is:
    - a laptop (+ keyboard, mouse & xtra monitor)
    - xp pro with apache & php
    - tortoisecvs for version control
    - gvim for editting
    - a live site registered with wiserhosting (25/year) where i can test in the real world

    rather minimalist??

    So, what are the benefits of virtual servers and virtual hosting?. What could i achieve with them, that i can't do now?

  11. #11
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by treacle0996 View Post
    So, what are the benefits of virtual servers and virtual hosting?. What could i achieve with them, that i can't do now?
    A VPS is basically a cheap alternative to a dedicated server, so you've got total control over the entire machine, and can run an unlimited number of sites (within reason). Granted, you basically JUST get root access to a default server install, so you have to configure and maintain the thing pretty much on your own, which can be daunting at first if you've never done it. Also, the "server" is virtual, so you only get like 10gb of storage and 256mb of ram for the $20/mo plan...not much for going live with a large site, but it's ideal for development, hosting small production sites, or just learning and doing experimental stuff. Obviously you can get the larger plans if you need the extra resources and have the extra money.

    BTW, I'm not trying to shill for Slicehost, I just use them and am happy with them...no contracts, no pre-paying for a year, no setup fee. It only takes like 10 minutes from the time you order to the time you get SSH access to your server.

    There are plenty of other VPS hosts out there though
    Drew C King: PHP Developer
    <?= $short_tags++ ?>

  12. #12
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    UK
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get it. The big advantage is it's all you. You can configure it as you please.

    Having configured unix/linux systems (even a little Xenix) previously i can see that is a huge benefit, especially if you are doing something a little out of the ordinary, or that requires extra security.

    If, however, a client wants really cheap hosting (like my 25 a year), or already has a hosting contract elsewhere, you couldn't guarantee the services you have configured on your own server will be available on theirs. I guess it depends what kind of clients you are developing for.

    At the moment (having only 2 clients!) my minimalist setup suits me fine. I'll hold off on the VPS until my clients requirements get a bit more interesting!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •