melloorr Posted December 11, 2011 Share Posted December 11, 2011 Okay, first things first. I have never done anything to URL's when coding n PHP, but... I want to make a redirect page, as in, if the user has met a certain condition, they are redirected to a different page, and they are given a specific error message depending on what the url is. (sorry if I have not explained it well.) But I do not know how to go about doing this. Any help at all please? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/ Share on other sites More sharing options...
QuickOldCar Posted December 11, 2011 Share Posted December 11, 2011 header() Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296761 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 Yes I know a header. But I want the user to be redirected to a page, then the page checks to see where the were redirected from, then displays the relevant error message. The page is a normal page (e.g. index) and will not always display the message Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296762 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 Use the test by 'if'. If all is OK, do redirect Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296764 Share on other sites More sharing options...
QuickOldCar Posted December 11, 2011 Share Posted December 11, 2011 From what you say, I guess you will be sending them to a page, lets say error.php that you made, that will then redirect them to your index.php page. Now on the index page you can use $_SERVER['HTTP_REFERER'] to find out where they just came from, if is from page error.php , then display a message. http://php.net/manual/en/reserved.variables.server.php Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296766 Share on other sites More sharing options...
QuickOldCar Posted December 11, 2011 Share Posted December 11, 2011 But instead of looking for where everyone came from all the time, it may be better to just delay the redirect, show the message and send them back to index.php <?php header( "refresh:5;url=index.php" ); echo "You'll be redirected in about 5 secs. If not, click <a href='index.php'>here</a>."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296773 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 From what you say, I guess you will be sending them to a page, lets say error.php that you made, that will then redirect them to your index.php page. Now on the index page you can use $_SERVER['HTTP_REFERER'] to find out where they just came from, if is from page error.php , then display a message. http://php.net/manual/en/reserved.variables.server.php I want't going to have an error page. it is for a login script, where, if the cookie id does not match the id in the database, then they are logged out. Here is a bit of pseudo code to show what i mean. //page.php if ($cookieid != database['cookie']) { header(Redirect: index.php/98eyux9dsn29dn; } //index.php while (redirect: 98eyux9dsn29dn) { echo "You have been logged out because your cookie has been compromised"; } Something along the lines of that Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296780 Share on other sites More sharing options...
QuickOldCar Posted December 11, 2011 Share Posted December 11, 2011 if that's the case, why not just this if($logged_in){ if ($cookieid != database['cookie']) { session_destroy(); header( "refresh:5;url=index.php" ); echo "You have been logged out because your cookie has been compromised"; } } Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296783 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 Works great! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296790 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 Sorry if this is off topic, but I want to disable php errors (or not display them) for a specific page on my website, is this possible? I am asking because if the cookie has been modified, then lots of undefined constants and variable errors appear. While it does not alter the way the script works, I just don't like the way it looks (I'm a bit OCD in that sense) Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296857 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 At the beginning of php file, write error_reporting(0); Or just write '@' before a variable For example @$var; // Or @$_COOKIE['name']; Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296864 Share on other sites More sharing options...
xyph Posted December 11, 2011 Share Posted December 11, 2011 Then you should check if those variables exist before using them. You should use isset Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296865 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 At the beginning of php file, write error_reporting(0); Thank you. It worked perfectly Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296866 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 Then you should check if those variables exist before using them. You should use isset I was getting the errors because I was using the cookie to get the username from the correct row in the database, so I could display it on screen. And if the cookie has been modified, then it has been destroyed, so the variable would not have been defined (if there was originally no cookie, then i do not get these errors, as the variable is only defined if the cookie is present) Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296871 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 Thank you. It worked perfectly But you must understand, that hiding errors, also untreated variables is very very bad ! Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296872 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 But you must understand, that hiding errors, also untreated variables is very very bad ! I know. I have only disabled errors for the following code if($_COOKIE) { error_reporting(0); if (isset($_COOKIE['Key_my_site']) == $cookiedbid) { echo "Your cookie is okay."; } elseif (isset($_COOKIE['Key_my_site']) !== $cookiedbid) { header( "refresh:5;url=index.php" ); echo "You have been logged out because your cookie has been compromised"; setcookie(Key_my_site, 0, $past); } else { echo "Your cookie is no longer there."; } error_reporting(1); } Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296873 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 (if there was originally no cookie, then i do not get these errors, as the variable is only defined if the cookie is present) For this, you must do this if(!isset($_COOKIE['name'])) set_cookie('name', 'value'); else // Do something with existing COOKIE Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296874 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 melloorr Your code is incorrect. Below, my code, it is correct error_reporting(E_ALL); if(isset($_COOKIE)) { if (isset($_COOKIE['Key_my_site']) && $_COOKIE['Key_my_site'] == $cookiedbid) echo "Your cookie is okay."; elseif (isset($_COOKIE['Key_my_site']) && $_COOKIE['Key_my_site'] !== $cookiedbid) { header("refresh:5;url=index.php" ); echo "You have been logged out because your cookie has been compromised"; setcookie(Key_my_site, 0, $past); } else echo "Your cookie is no longer there."; } Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296875 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 Yeah I was thinking that too. But the error_reporting(E_ALL) should have been at the bottom, not the top. And even without the error_reporting(E_ALL) at the botton, error would still only be turned off for that if statement only Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296877 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 (if there was originally no cookie, then i do not get these errors, as the variable is only defined if the cookie is present) For this, you must do this if(!isset($_COOKIE['name'])) set_cookie('name', 'value'); else // Do something with existing COOKIE I do not understand this. That code is saying, if there is no cookie, then set one? Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296879 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 That code is saying, if there is no cookie, then set one? Yes. The code was for example Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296881 Share on other sites More sharing options...
melloorr Posted December 11, 2011 Author Share Posted December 11, 2011 Yes. The code was for example I know but the variable is set in a different file, and the whole point of this is to check if the if in the cookie is the same as the one in the database, and if it is not, then the cookie is deleted and they are logged out due to the cookie being deleted. All the code works as it is, they are logged out if the cookie is not correct, and they will have to log in again. It is all secure (i think). I may upload it to be beta tested But thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/252935-url-help/#findComment-1296887 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.