joecooper Posted October 13, 2008 Share Posted October 13, 2008 as the title, i have a script that should only be run when it is clicked though a link from the index.php page, otherwise display error. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/ Share on other sites More sharing options...
Zane Posted October 13, 2008 Share Posted October 13, 2008 please don't double post... Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664486 Share on other sites More sharing options...
joecooper Posted October 13, 2008 Author Share Posted October 13, 2008 i posted my first in the wrong section, i noticed and asked for it to be deleted and posted it in the correct place. please dont waste my time Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664489 Share on other sites More sharing options...
Lamez Posted October 13, 2008 Share Posted October 13, 2008 Session Variables When they visit the index.php assign a unique session variable, then on the other page is the variable is not set redirect them\give them an error. Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664490 Share on other sites More sharing options...
Slip Posted October 13, 2008 Share Posted October 13, 2008 You could just check the $_SERVER['HTTP_REFERER'] variable like this; if($_SERVER['HTTP_REFERER'] != 'index.php') { echo "Error. I'm going to stop working now..."; return; } // Now carry on with the rest of your script... Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664509 Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 The referrer is not always set, and can be forged. Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664519 Share on other sites More sharing options...
sylvertwst Posted October 13, 2008 Share Posted October 13, 2008 maybe change the link to an input(on index.php) and set a variable that you can call to check if the user came from the index on the next page? Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664522 Share on other sites More sharing options...
joecooper Posted October 14, 2008 Author Share Posted October 14, 2008 ive gone for the http_referer one for now. but the link that it clicks though might be index.php?module=something and so doent always work. Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664576 Share on other sites More sharing options...
kenrbnsn Posted October 14, 2008 Share Posted October 14, 2008 Use sessions -- much more reliable than the http_referer In the index.php script: <?php session_start(); $_SESSION['coming_from'] = 'index.php'; ?> In the second script: <?php session_start(); if ($_SESSION['coming_from'] != 'index.php') header('location: index.php'); //put them back to index.php ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664579 Share on other sites More sharing options...
Lamez Posted October 14, 2008 Share Posted October 14, 2008 Yes, I suggested a good idea! Quote Link to comment https://forums.phpfreaks.com/topic/128281-only-run-script-if-page-was-accessed-via-indexphp/#findComment-664605 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.