관련링크 2번째에 랜덤으로 링크 뿌려주기

관련링크 2번째에 랜덤으로 링크 뿌려주기

QA

관련링크 2번째에 랜덤으로 링크 뿌려주기

본문

setup.php 는 아래처럼

link_1 = "https://link1.com";
link_2 = "https://link2.com";
link_3 = "https://link3.com";
link_4 = "https://link4.com";
link_5 = "https://link5.com";
link_6 = "https://link6.com";
link_7 = "https://link7.com";
link_8 = "https://link8.com";
link_9= "https://link9.com";
link_10 = "https://link10.com";

 

A사이트에 올려두고

 

B사이트에서 A사이트의 주소를 랜덤으로 관련 링크2에 넣어줄수 있는 방법이 있을까요? 새로고침 할때마다 주소는 랜덤으로 계속 바뀌게요.

 

1번 사이트에 링크주소를 여려개 올린다.

 

2번 사이트에서 1번사이트에 링크를 랜덤하게 가져와 관련링크 2번째 란에 넣어준다.

 

링크값은 새로고침 할때마다 랜덤하게 바뀐다. 

 

https://joytv77.store/newmv/%EC%84%9C%EC%9A%B8%EC%9D%98-%EB%B4%84/

(예시 사이트 입니다)

이 질문에 댓글 쓰기 :

답변 4

curl을 이용해서 링크를 가져오는 것이 정상절차인데 자바스크립트를 이용해도 됩니다

setup.php

<?php
$linkarr=["https://link1.com",
        "https://link2.com",
        "https://link3.com",
        "https://link4.com",
        "https://link5.com",
        "https://link6.com",
        "https://link7.com",
        "https://link8.com",
        "https://link9.com",
        "https://link10.com"];

shuffle($linkarr);
echo "link ='{$linkarr[0]}'";     

 

===============view에서

            <?php
                //링크
                $cnt = 0;

$view['link'][2] = "link2"; $view['link_href'][2] = "link2"; $view['link_hit'][2] =0;//----------추가
                for ($i=1; $i<=count($view['link']); $i++) {
                   ?>
                    <div class="d-flex my-1">
                        <div class="px-0">
                            <i class="fa fa-link" aria-hidden="true"></i>   
                        </div>
                        <div class="pl-3 flex-grow-1 text-break-all">
                          <a href="<?php echo $view['link_href'][$i] ?>" target="_blank" id="link<?php echo $i?>"> <------- id 추가 
                                <?php echo get_text($view['link'][$i]) ?>
                                <?php if($view['link_hit'][$i]) { ?>

 

--------------하단 자바스크립트

<script src="http://사이트.com/setup.php"></script>
<script>  
$("#link2").attr("href", link);
 $("#link2").text(link)
</script>



		<?php if(isset($view['link'][1]) && $view['link'][1]) { ?>

		 <!-- 관련링크 시작 { -->
        <li class="d-table-row border-top border-bottom">
            <div class="d-none d-sm-table-cell text-center px-3 py-2 nw-6">
                링크
            </div>
            <div class="d-table-cell px-3 py-2">
                <?php

                //링크
                $cnt = 0;

$view['link'][2] = "link2"; $view['link_href'][2] = "link2"; $view['link_hit'][2] =0;//----------추가
                for ($i=1; $i<=count($view['link']); $i++) {
                   ?>
                    <div class="d-flex my-1">
                        <div class="px-0">
                            <i class="fa fa-link" aria-hidden="true"></i>   
                        </div>
                        <div class="pl-3 flex-grow-1 text-break-all">
                          <a href="<?php echo $view['wr_link2'][$i] ?>" target="_blank" id="link<?php echo $i?>"> 
                                <?php echo get_text($view['link'][$i]) ?>
                                <?php if($view['link_hit'][$i]) { ?>
 
                    <div class="d-flex my-1">
                        <div class="px-0">
                            <i class="fa fa-link" aria-hidden="true"></i>   
                        </div>
                        <div class="pl-3 flex-grow-1 text-break-all">
                          <a href="<?php echo $view['wr_link2'][$i] ?>" target="_blank">
                                <?php echo get_text($view['wr_link2'][$i]) ?>
                                <?php if($view['link_hit'][$i]) { ?>
                                    <span class="count-plus orangered"><?php echo $view['link_hit'][$i] ?></span>
                                    <span class="sr-only">회 연결</span>
                                <?php } ?>
                            </a>    
                        </div>
                    </div>
                    <?php
                    }
                }
                ?>
            </div>
        </li>
        <!-- } 관련링크 끝 -->

		<script src="http://***.kr/setup.php"></script>
<script>  
$("#link2").attr("href", link);
 $("#link2").text(link)
</script> 



어디가 잘못된걸까요?;;

A-site setup.php


<?php
$wrap = [
    'links' => [
        "https://link1.com",
        "https://link2.com",
        "https://link3.com",
        "https://link4.com",
        "https://link5.com",
        "https://link6.com",
        "https://link7.com",
        "https://link8.com",
        "https://link9.com",
        "https://link10.com",
    ]
];
 
echo json_encode($wrap);
?>

 

B-site


<?php
$content = file_get_contents('http://A-site.com/setup.php');
if ($content !== false) {
    $data = json_decode($content, true);
    $links = $data['links'];
    $link = mt_rand(0, count($links) - 1);
    echo $links[$link];
}
?>

게시판 관련링크 쪽에 랜덤링크를 보여주게 하려고 합니다.


	    <!-- 관련링크 시작 { -->
		<li class="d-table-row border-top border-bottom">
			<div class="d-none d-sm-table-cell text-center px-3 py-2 nw-6">
				링크
			</div>
			<div class="d-table-cell px-3 py-2">
				<?php
				//링크
				$cnt = 0;
				for ($i=1; $i<=count($view['link']); $i++) {
					if ($view['link'][$i]) {
						$cnt++;
					?>
					<div class="d-flex my-1">
						<div class="px-0">
							<i class="fa fa-link" aria-hidden="true"></i>	
						</div>
						<div class="pl-3 flex-grow-1 text-break-all">
							<a href="<?php echo $view['link_href'][$i] ?>" target="_blank">
								<?php echo get_text($view['link'][$i]) ?>
								<?php if($view['link_hit'][$i]) { ?>
									<span class="count-plus orangered"><?php echo $view['link_hit'][$i] ?></span>
									<span class="sr-only">회 연결</span>
								<?php } ?>
							</a>	
						</div>
					</div>
					<?php
					}
				}
				?>
			</div>
		</li>
	    <!-- } 관련링크 끝 -->

뷰스킨 에서 관련링크에 삽입을 하고 싶습니다.

다음 예시코드를 참조해 적절히 가공하면 될것 같습니다.


<?php
$content = file_get_contents('http://A-site.com/setup.php');

$other_links = [''];

if ($content !== false) {
    $data = json_decode($content, true);
    $other_links = $data['links'];
    // $other_link = mt_rand(0, count($other_links) - 1);
    // echo $other_links[$other_link];
}
?>

        <!-- 관련링크 시작 { -->
        <li class="d-table-row border-top border-bottom">
            <div class="d-none d-sm-table-cell text-center px-3 py-2 nw-6">
                링크
            </div>
            <div class="d-table-cell px-3 py-2">
                <?php
                //링크
                $cnt = 0;
                for ($i=1; $i<=count($view['link']); $i++) {
                    if ($view['link'][$i]) {
                        $cnt++;

                        if ($i == 2) {
                            $other_link = mt_rand(0, count($other_links) - 1);
                            $view['link_href'][$i] = $other_links[$other_link];
                            $view['link'][$i] = $other_links[$other_link];
                        }
                    ?>
                    <div class="d-flex my-1">
                        <div class="px-0">
                            <i class="fa fa-link" aria-hidden="true"></i>   
                        </div>
                        <div class="pl-3 flex-grow-1 text-break-all">
                            <a href="<?php echo $view['link_href'][$i] ?>" target="_blank">
                                <?php echo get_text($view['link'][$i]) ?>
                                <?php if($view['link_hit'][$i]) { ?>
                                    <span class="count-plus orangered"><?php echo $view['link_hit'][$i] ?></span>
                                    <span class="sr-only">회 연결</span>
                                <?php } ?>
                            </a>    
                        </div>
                    </div>
                    <?php
                    }
                }
                ?>
            </div>
        </li>
        <!-- } 관련링크 끝 -->

다음처럼 주석처리를 하면 2번째 링크만이 아닌 모든 링크가 교체됩니다.


                        //if ($i == 2) {
                            $other_link = mt_rand(0, count($other_links) - 1);
                            $view['link_href'][$i] = $other_links[$other_link];
                            $view['link'][$i] = $other_links[$other_link];
                        //}


<?php
$content = file_get_contents('https://site.com/setup.php');

$other_links = [''];

if ($content !== false) {
    $data = json_decode($content, true);
    $other_links = $data['links'];
    // $other_link = mt_rand(0, count($other_links) - 1);
    // echo $other_links[$other_link];
}
?>
		 <!-- 관련링크 시작 { -->
        <li class="d-table-row border-top border-bottom">
            <div class="d-none d-sm-table-cell text-center px-3 py-2 nw-6">
                링크
            </div>
            <div class="d-table-cell px-3 py-2">
                <?php
                //링크
                $cnt = 0;
                for ($i=2; $i<=count($view['link']); $i++) {
                    if ($view['link'][$i]) {
                        $cnt++;

                      //if ($i == 2) {
                            $other_link = mt_rand(0, count($other_links) - 1);
                            $view['link_href'][$i] = $other_links[$other_link];
                            $view['link'][$i] = $other_links[$other_link];
                       // }
                    ?>
                    <div class="d-flex my-1">
                        <div class="px-0">
                            <i class="fa fa-link" aria-hidden="true"></i>   
                        </div>
                        <div class="pl-3 flex-grow-1 text-break-all">
                          <a href="<?php echo $view['link_href'][$i] ?>" target="_blank">
                                <?php echo get_text($view['link'][$i]) ?>
                                <?php if($view['link_hit'][$i]) { ?>
                                    <span class="count-plus orangered"><?php echo $view['link_hit'][$i] ?></span>
                                    <span class="sr-only">회 연결</span>
                                <?php } ?>
                            </a>    
                        </div>
                    </div>
                    <?php
                    }
                }
                ?>
            </div>
        </li>
        <!-- } 관련링크 끝 -->


아무런 내용도 출력이 되지 않습니다.ㅠ.ㅠ
셋업파일 주소부분은 제대로 넣고 했는데 아무것도 출력이 되지 않습니다.ㅠ.ㅠ

먼저 기존 링크가 존재 하는지 확인이 필요할것 같고


if (count($view['link']) > 0) {
    echo 'link count ' . count($view['link']);
} else {
    echo 'link count 0';
}


다른곳에서 가져오는 방식 대신 다음처럼 확인해볼수도 있습니다.

//$content = file_get_contents('http://A-site.com/setup.php');

//$other_links = [''];
$other_links = [
    "https://link1.com",
    "https://link2.com",
    "https://link3.com",
    "https://link4.com",
    "https://link5.com",
    "https://link6.com",
    "https://link7.com",
    "https://link8.com",
    "https://link9.com",
    "https://link10.com",
];

//if ($content !== false) {
//    $data = json_decode($content, true);
//    $other_links = $data['links'];
//    // $other_link = mt_rand(0, count($other_links) - 1);
//    // echo $other_links[$other_link];
//}


<?php

//$content = file_get_contents('http://A-site.com/setup.php');

//$other_links = [''];
$other_links = [
    "https://link1.com",
    "https://link2.com",
    "https://link3.com",
    "https://link4.com",
    "https://link5.com",
    "https://link6.com",
    "https://link7.com",
    "https://link8.com",
    "https://link9.com",
    "https://link10.com",
];

//if ($content !== false) {
//    $data = json_decode($content, true);
//    $other_links = $data['links'];
//    // $other_link = mt_rand(0, count($other_links) - 1);
//    // echo $other_links[$other_link];
//}
?>


		 <!-- 관련링크 시작 { -->
        <li class="d-table-row border-top border-bottom">
            <div class="d-none d-sm-table-cell text-center px-3 py-2 nw-6">
                링크
            </div>
            <div class="d-table-cell px-3 py-2">
                <?php
                //링크
                $cnt = 0;
                for ($i=2; $i<=count($view['link']); $i++) {
                    if ($view['link'][$i]) {
                        $cnt++;

                   if ($i == 2) {
                            $other_link = mt_rand(0, count($other_links) - 1);
                            $view['link_href'][$i] = $other_links[$other_link];
                            $view['link'][$i] = $other_links[$other_link];
                     }
                    ?>
                    <div class="d-flex my-1">
                        <div class="px-0">
                            <i class="fa fa-link" aria-hidden="true"></i>   
                        </div>
                        <div class="pl-3 flex-grow-1 text-break-all">
                          <a href="<?php echo $view['link_href'][$i] ?>" target="_blank">
                                <?php echo get_text($view['link'][$i]) ?>
                                <?php if($view['link_hit'][$i]) { ?>
                                    <span class="count-plus orangered"><?php echo $view['link_hit'][$i] ?></span>
                                    <span class="sr-only">회 연결</span>
                                <?php } ?>
                            </a>    
                        </div>
                    </div>
                    <?php
                    }
                }
                ?>
            </div>
        </li>
        <!-- } 관련링크 끝 -->


위와같이 해봐도 아무런것도 뜨질 않네요.ㅠ.ㅠ

for ($i=1 이 for ($i=2 로 변경된것 같습니다.


                <?php
                //링크
                $cnt = 0;
                //for ($i=2; $i<=count($view['link']); $i++) {
                for ($i=1; $i<=count($view['link']); $i++) {
                    if ($view['link'][$i]) {
                        $cnt++;

                   //if ($i == 2) {
                            $other_link = mt_rand(0, count($other_links) - 1);
                            $view['link_href'][$i] = $other_links[$other_link];
                            $view['link'][$i] = $other_links[$other_link];
                     //}
                    ?>


                <?php
               //링크
                $cnt = 0;
                //for ($i=2; $i<=count($view['link']); $i++) {
                for ($i=1; $i<=count($view['link']); $i++) {
                    if ($view['link'][$i]) {
                        $cnt++;

                 // if ($i == 2) {
                            $other_link = mt_rand(0, count($other_links) - 1);
                            $view['link_href'][$i] = $other_links[$other_link];
                            $view['link'][$i] = $other_links[$other_link];
                    // }
                    ?>

이렇게 했을때에는 아무런것도 출력이 되지 않습니다.

                <?php
               //링크
                $cnt = 0;
                //for ($i=2; $i<=count($view['link']); $i++) {
                for ($i=1; $i<=count($view['link']); $i++) {
                    if ($view['link'][$i]) {
                        $cnt++;

                 if ($i == 2) {
                            $other_link = mt_rand(0, count($other_links) - 1);
                            $view['link_href'][$i] = $other_links[$other_link];
                            $view['link'][$i] = $other_links[$other_link];
                     }
                    ?>

이렇게 했을때에는 링크1이 출력이 되네요.ㅠ.ㅠ

기존링크 1개를 임의설정하고 랜덤링크를 추가하는
다음 예시코드가 도움이 될지 모르겠습니다.


<?php
$other_links = [
    "https://link1.com",
    "https://link2.com",
    "https://link3.com",
    "https://link4.com",
    "https://link5.com",
    "https://link6.com",
    "https://link7.com",
    "https://link8.com",
    "https://link9.com",
    "https://link10.com",
];

$view['link'] = ['1' => 'http://link.com'];
$view['link_href'] = ['1' => 'http://link.com'];

for ($i = 0; $i < 2; $i++) {
	if (isset($view['link'][$i + 1]) == false) {
		$other_link = mt_rand(0, count($other_links) - 1);
		$view['link'][$i + 1] = $other_links[$other_link];
		$view['link_href'][$i + 1] = $other_links[$other_link];
	}
}
?>

                <?php
               //링크
                $cnt = 0;
                //for ($i=2; $i<=count($view['link']); $i++) {
                for ($i=1; $i<=count($view['link']); $i++) {
                    if ($view['link'][$i]) {
                        $cnt++;
                    ?>
                <a href="<?php echo $view['link_href'][$i]; ?>"><?php echo $view['link'][$i]; ?></a>
                <?php
                    }
                }
                ?>

저렴한 웹호스팅 중에는 php 의 file_get_contents 나 curl 을 허용해 주지 않는 데가 꽤 있습니다.

그러니 사이트를 랜덤으로 안전하게 튕기려면

프론트에서 자바스크립트로 튕기는 것이 코드는 길지만 더 효과가 확실합니다. 

즉 저 변수들을 js 파일안에 심어 놓고 불러와야 안전하지 php 파일에 심어 놓으면 cutl 등이 막혀 있으면 당연히 안 될 것입니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 425
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT