1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help for redirect by sub-ID with PHP and SQL

Discussion in 'PHP & Perl' started by darkstar69, Mar 12, 2012.

  1. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    Hello,
    I want to create a script that redirects the users based on the sub-ID.
    I already made the script in PHP but it´s very slow when it has to handle many sub-ID´s so I thought maybe it´s faster when I use it together with a SQL database.

    Right now this the script I made:

    Code:
    <?php
    $subid = $_GET['subid'];
    
    if (preg_match('/subid1/i', $subid)) {
      header('Location: http://mysite.com/subid1');
    } 
    elseif(preg_match('/subid2/i', $subid)) {
    Header('Location: http://mysite.com/subid2');
    }
    elseif(preg_match('/subid3/i', $subid)) {
    Header('Location: http://mysite.com/subid3');
    }
    elseif(preg_match('/subid4/i', $subid)) {
    Header('Location: http://mysite.com/subid4');
    }
    elseif(preg_match('/subid5/i', $subid)) {
    Header('Location: http://mysite.com/subid5');
    }
    else {Header('Location: http://mysite.com');
    }
    ?>
    I thought about something like that:

    Code:
    CREATE TABLE `subid` (
       `subid` varchar(100),
       `redirect` varchar(100),
       PRIMARY KEY(subid)
    );
    INSERT INTO subid VALUES('subid1','http://mysite.com/subid1');
    INSERT INTO subid VALUES('subid2','http://mysite.com/subid2');
    INSERT INTO subid VALUES('subid3','http://mysite.com/subid3');
    INSERT INTO subid VALUES('subid4','http://mysite.com/subid4');
    INSERT INTO subid VALUES('subid5','http://mysite.com/subid5');
    And then combinate it with the PHP script so that the script gets the subid from the url, connects to the database, fetches the $subid together with the appropriate redirect from the DB and paste it into the script.

    I hope that makes sense for you and I´m sorry that I couldn´t serve more code but I´m no coder at all and already tried my best to get it done myself.

    Thanks in advance.
     
  2. Xp3r7

    Xp3r7 Regular Member

    Joined:
    Sep 9, 2011
    Messages:
    286
    Likes Received:
    386
    Occupation:
    Freelancer
    Location:
    In the middle of nowhere
    Code:
    Header('Location: http://mysite.com/'.$subid)
    ?
     
  3. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    How many SubIDs do you have?
    strpos() will probably be faster than preg_match();
     
  4. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    I will change the script a little bit to make it more obvious:

    Code:
    <?php
    $subid = $_GET['subid'];
    
    if (preg_match('/subid1/i', $subid)) {
      header('Location: http://mysite1.com');
    } 
    elseif(preg_match('/subid2/i', $subid)) {
    Header('Location: http://mysite2.com');
    }
    elseif(preg_match('/subid3/i', $subid)) {
    Header('Location: http://mysite3.com');
    }
    elseif(preg_match('/subid4/i', $subid)) {
    Header('Location: http://mysite4.com');
    }
    elseif(preg_match('/subid5/i', $subid)) {
    Header('Location: http://mysite5.com');
    }
    else {Header('Location: http://mysite.com');
    }
    ?>

    I don´t exactly know how many.
    Maybe 80 or 90 or more
     
  5. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    Maybe you can use something like this?

    PHP:
    <?php
    $subid1 
    'http://example.com/1';
    $subid2 'http://example.com/2';
    $subid3 'http://example.com/3';

    $subid $_GET['subid'];

    header('Location: ' . $$subid);
    ?>
     
  6. Xp3r7

    Xp3r7 Regular Member

    Joined:
    Sep 9, 2011
    Messages:
    286
    Likes Received:
    386
    Occupation:
    Freelancer
    Location:
    In the middle of nowhere
    Code:
    Header('Location: http://mysite'.$subid.'.com');
    This should work.
     
    • Thanks Thanks x 1
  7. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    Sorry, but when with $subid you fetch the subid from the url (?subid=something) I don´t see how your code chooses whether to insert $subid1, $subid2 or $subid3 and then where to redirect based on the given subid.
    Sorry again, but I think you just don´t get what I mean.
    The redirects shall go to completely different sites.
    Here´s a better example again (at least I hope so):

    Code:
    <?php
    $subid = $_GET['subid'];
    
    if (preg_match('/subid1/i', $subid)) {
      header('Location: http://abc.com');
    } 
    elseif(preg_match('/subid2/i', $subid)) {
    Header('Location: http://def.com');
    }
    elseif(preg_match('/subid3/i', $subid)) {
    Header('Location: http://ghi.com');
    }
    elseif(preg_match('/subid4/i', $subid)) {
    Header('Location: http://jkl.com');
    }
    elseif(preg_match('/subid5/i', $subid)) {
    Header('Location: http://mno.com');
    }
    else {Header('Location: http://pqr.com');
    }
    ?>
    Your code would not work anyway.
    Let´s say the url is http://example.com?subid=thisisatest
    Then your code would redirect to

    Code:
    Header('Location: http://mysite'.$subid.'.com');
    which would be

    Code:
    Header('Location: http://mysitethisisatest.com');
    Oh well, once again I see the disadvantage I have because of my lack of coding skills...
     
  8. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,155
    From the top of my head, may have errors :)

    Code:
    $mySid = $_GET['subid'];
    
    $sites2sid = array (
        'http://www.siteA.com' => 'sid1',
        'http://www.siteB.com' => 'sid2'
    );
    
    if ($key = array_search($mySid, $sites2sid) ) {
        header('Location: '. $key);
    }
    
     
    • Thanks Thanks x 1
  9. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    Notice the double dollar ($$subid) sign in the header function.
    You can read about it here. Again, not sure how good this is performance-wise.
     
    • Thanks Thanks x 1
  10. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    Thanks, will have a look at it and see if I can use it.
    Basically first I have to see if I full understand your code.
    But I think I read somethign about arrays before and think that could be better/faster then SQL stuff.
    So let´s forget about the thread titel, no SQL anymore :)


    Ah, I thought the double dollar was a typo.
    Will read about it now.
    Slowly I really think that I should just try to learn PHP...
    At least a little bit.
     
    Last edited: Mar 12, 2012
  11. tripper_john_md

    tripper_john_md Newbie

    Joined:
    Feb 21, 2011
    Messages:
    40
    Likes Received:
    35
    Location:
    Southern Germany
    I would've done it vice versa, but jazzc's solution will work.
    PHP:
    $subid=$_GET['subid'];
    $id2site = array (
         
    'sid1' => 'url1',
         
    'sid2' => 'url2'
    );
    if (!empty(
    $id2site[$subid])) {
         
    header('Location: '.$id2site[$subid]);
    }
    else{
         
    //do something else
    }
    ?>
     
    • Thanks Thanks x 2
  12. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    I just see you come from germany.
    I´m from germany myself.
    So do you have skype?
    Then I could tell you in german what I plan to do, I think that would be much easier.
     
  13. tripper_john_md

    tripper_john_md Newbie

    Joined:
    Feb 21, 2011
    Messages:
    40
    Likes Received:
    35
    Location:
    Southern Germany
    I've skype, just pm me your name ;)
     
  14. tripper_john_md

    tripper_john_md Newbie

    Joined:
    Feb 21, 2011
    Messages:
    40
    Likes Received:
    35
    Location:
    Southern Germany
    I don't like it when things stay open, so...:
    We've done it with MySQL and a little modification of jazzc's and mine code examples. There are many sub-ids and it would have been absolutely uncomfortable to manage them inside the script.

    Every example would've worked, and it's nice to see several different approaches to a solution. :)
     
    • Thanks Thanks x 2