Jump to content

problem w/ get variable


Xdega

Recommended Posts

So. I wrote this class that redirects a user to a new url:

 

<?php
class PotentGate{
	private $endURL, $sourceURL = 'http';

		private function Gate_setURL(){
			//THE BLOCK BELOW WIL ULTIMATELY REFERECE A DATABASE BACKEND FOR DESTINATION URLS.
			if ($this->sourceURL=="http://blah.com/blah/misc.php"){$this->endURL="http://blah.com/blah/misc2.php?r=" . $this->Gate_sourceURL;}
			else {}
		}

		public function Gate_getURL(){
			$this->Gate_sourceURL();
			echo $this->sourceURL . "<br />";

			$this->Gate_setURL(); //set our destination URL based on source
			echo $this->endURL . "<br />";

			return header('Location:' . $this->endURL); //redirect 
		}

		private function Gate_sourceURL() {
				//TODO: Fix the $_SERVER['HTTPS'] below for https://  support. 
				//if ($_SERVER['HTTPS'] == "on") {$this->sourceURL .= "s";}

				$this->sourceURL .= "://";
				if ($_SERVER['SERVER_PORT'] != "80") {
					$this->sourceURL .= $_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
				} 	
				else {
						$this->sourceURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
					}
				return $this->sourceURL;
			}
}//end

 

It is implemented as such:

 

<?php
include("PotentGate.php");
$gate = new PotentGate();
$gate->Gate_getURL();  

//IGNORE
//function below will acquire get variable
//echo "You came from:" . $gate->Gate_getReferrer();

?>

 

This works all well and good, as far as redirection goes, but refuses to add the $Gate_sourceURL to the end url.

I am left with a url like: http://blah.com/blah/misc2.php?r=

and what I am trying to achieve is that it assign $Gate_sourceURL to r (so I can use the source url as a value later on), as opposed to be being blank.

ex.

http://blah.com/blah/misc2.php?r=http://blah.com/blah/misc1.php

 

Any Ideas?

Link to comment
https://forums.phpfreaks.com/topic/262319-problem-w-get-variable/
Share on other sites

Please format your code better, control structures all on one line is hard to read or understand.

 

 

				if ($this->sourceURL=="http://blah.com/blah/misc.php"){$this->endURL="http://blah.com/blah/misc2.php?r=" . $this->Gate_sourceURL;}

You forgot the parens after Gate_sourceURL. 

 

Since you didn't see the error, that means you're running without error-reporting turned on, which is bad.

 

You also have a terrible naming convention.  You should stick with either camelCase or underscore_separated, not mix_andMatch.  You'll never remember how to write all of these variables once your code is more than a single page.

 

Yay It's working.

Thanks for that, in terms of the formatting, I am a complete novice (as you can probably tell.), my apologies on that.

 

I did go ahead and add the parens, but also noticed another blunder. I was using $this->Get_SourceURL, instead of

$this->source URL  /doh!

 

I went ahead and reformatted my method names as such:

GateSetURL()

GateGetURL()

GateSourceURL()

Is this ok?

 

Should I also Camel case my private members?

$endURL, $sourceURL

 

Also, any specific formatting advice you could share to help with my issue would be great. I am working on this project mostly solo atm, but it will be part of a larger project with some co-ed friends of mine (hence the reason I added the Gate prefix to my methods). So anything I can do to make the code cleaner, and to improve my development practices in general would be of great benefit.

 

Thank you much for your help.

 

 

 

 

Ok. I went ahead and reformatted my code for the class file.

Would this be considered clean code? Are there any other things that I would want to adjust?

In terms of the functionality, it is doing exactly what I am intending atm.

Thanks so much for the help thus far.

 

 

<?php

class PotentGate{
	private $EndURL, $SourceURL = 'http';

		private function GateSetURL(){
			//The block below will ultimately reference a DB backend. 
			//TODO: Implement a database layer to define the rerirect URL based on Source URL.
			if ($this->SourceURL=="http://foo/bar/misc.php"){
					$this->EndURL="http://foo/bar/misc2.php?r=" . $this->SourceURL;
				}
			else {
				//no redirect defined
			}
		}

		public function GateGetURL(){
			$this->GateSourceURL(); //aqcuire our source URL
			$this->GateSetURL(); //set our destination URL based on source
			return header('Location:' . $this->EndURL); //redirect 
		}


		public function GateGetReferrer(){
			$RefURL=$_GET['r'];
				if ($this->GateValidateURL($RefURL)){
					return $RefURL;
				}
				else{
					return 'Invalid/Undefined URL.';
				}
		}


		private function GateSourceURL() {
				//TODO: Fix the $_SERVER['HTTPS'] below for https://  support. 
				//if ($_SERVER['HTTPS'] == "on") {$this->sourceURL .= "s";}

				$this->SourceURL .= "://";
				if ($_SERVER['SERVER_PORT'] != "80") {
					$this->SourceURL .= $_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
				} 	
				else {
						$this->SourceURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
					}
				return $this->SourceURL;
			}

		private function GateValidateURL($URL) {
    				$Pattern = "/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";
    				return (bool)preg_match($Pattern, $URL);
			}

}//end

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.