Grow your CSS skills. Land your dream job.

HELP: PHP redirct within a certain div rather than an entire site page

  • # November 13, 2012 at 9:57 am

    I manage my schools robotics team website but i cant embed youtube videos onto the website without the school blocking it. they have agreed to not block it if i can specify for the videos to not be shown if the IP if the ip that the school uses for student computers.
    Can i make a specific content div within a page display a youtube video if an ip is anything except for the 1 specific ip that all of the school computers have? also how would i have it display something besides the video like an error image instead of the video.

    i have fundamental PHP knowledge and i assume this is what is involved.
    Alternativly i can have the page redirect to another page on my website like http://www.website.com/content-alt rather than /content. which way would be an easier/better way to do this and could someone help me write the code for this?

    THANKS!

    # November 13, 2012 at 10:49 am

    You don’t need to redirect at all.

    Check the users IP and write the embed code if it’s good; write an error message if it’s not. You can find the user’s IP in `$_SERVER`.

    Give it a try, ask if you need help!

    # November 13, 2012 at 10:50 am

    Hey @tylerrichardson96

    Sounds quite strange the way you would have to go through all that just to get a YouTube video to work on your website.

    What you could do however is open YouTube, search for the video and click it, then grab the URL of that video and copy it.

    Once you have that you could head on over to http://www.keepvid.com and enter the URL in the address bar on there. Save it in a different format (for example – .FLV) and download it to your computer.

    Then you could just use a plugin such as [FlowPlayer](http://flowplayer.org/ “”) and embed it that way?

    # November 13, 2012 at 1:34 pm

    @traq, im not sure how to write the code at all, ive done a bit of research and i found this redirect based on ip:

    < ?php
    $allowed = array(“94.171.121.121″);
    $ip = $_SERVER;
    if(!in_array($ip, $allowed))
    {
    header(“Location: http://www.google.co.uk); /* Redirect browser */
    /* Make sure that code below does not get executed when we redirect. */
    exit;
    }
    else
    {

    }
    ?>

    how can i manipulate this to make it apply to a single element (my div with the youtube video) rather than the entire page of the website?

    # November 13, 2012 at 2:43 pm

    As I said before, you don’t. There’s no need for redirecting anyone. IIUC, this is a simple `if` – `else` situation.

    IF the visitor is on a particular IP address, you want to show the video.

    ELSE (i.e., *if not*), you want to show an error message.

    Correct?


    < ?php

    // this is the IP of the visitor to your site.
    $userIP = $_SERVER;

    // this is the "allowed" IP.
    $goodIP = '000.000.000.000';

    if( $userIP == $goodIP ){
    // the user is on the allowed IP.
    // show the video.
    print '';
    }else{
    // the user is not on the allowed IP.
    // show something else.
    print "Sorry, you can't watch this video.";
    }
    ?>

    If you had several “good” IPs, you could put them in an array and use `in_array()` as in the example you found, but you still wouldn’t need to redirect the user.

    # November 14, 2012 at 2:54 pm

    Thanks so much!!! 1 last question, how can i have it display something else instead of the video, i assume i would put it after the 2nd print, but what is the php syntax for ‘printing’ a picture?

    # November 14, 2012 at 6:56 pm

    These is no such special syntax.

    PHP is not an “alternative” to HTML; PHP *writes* HTML.

    < ?php
    print ‘my picture‘;

    (Yes, it would be inside the `else` block.)

    # November 19, 2012 at 8:23 am

    Thanks so much :) i put the code into my website and this is what is looks like http://www.firstteam3600.com/videos i dont have my blocked ip yet so i have both ip events as the same thing. can you tell me why the code is showing up in the website?

    # November 19, 2012 at 10:58 am

    My guess is that you haven’t closed a tag or something.

    FIRST Videos

    < ?php

    $userIP = $_SERVER;
    $badIP = ’000.000.000.000′;
    if( $userIP == $badIP ){
    print ‘‘;
    }
    else{
    print ‘‘;
    }
    ?>

     

    # November 19, 2012 at 8:52 pm

    > this is what is looks like http://www.firstteam3600.com/videos

    actually, I get a 404.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".