Jump to content

URL help


melloorr

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>.";
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

(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

Link to comment
Share on other sites

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.";
}

Link to comment
Share on other sites

(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?

Link to comment
Share on other sites

 

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.