check if server response is redirect

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply
Jason_C
New php-forum User
New php-forum User
Posts: 47
Joined: Sat Nov 18, 2006 7:32 am

Tue Apr 12, 2016 3:07 am

I have a form where someone enters a URL, I wish to find out if a website that is entered is the main website and not a site that the entered URL is redirected to.

say someone enters http://www.mysite.com
but this site is the old URL and I have a new site where the old site is redirect to being... http://www.mynewsite.com this URL is now in the address bar.

I wish to get the server response and get the new site that they are redirected to.

How would I do this so I can place an message on the screen that the url they entered is being redirected and to enter the correct url being the site site?

I have been playing around with code for a bit and come up with this...

Code: Select all

var Response = '';
function loadXMLDoc(url) {
						var xmlhttp;
							if (window.XMLHttpRequest) {
							xmlhttp=new XMLHttpRequest();	// code for IE7+, Firefox, Chrome, Opera, Safari
							} else {
							xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");	// code for IE6, IE5
							}
								//xmlhttp.onreadystatechange=function() {
									//if (xmlhttp.readyState==4 && xmlhttp.status==200) {
									//Response = xmlhttp.responseText;
									Response = xmlhttp.status;
									//}
								//}
						xmlhttp.open("GET",url,true);
						xmlhttp.send();
						}
function update() {
document.getElementById('q').innerHTML=Response;
alert('.' + Response + '.');
}

Code: Select all

<br><br>URL: must include the http://<br><input type="text" style="width: 250px;" name="aUrl" value="<?php echo ((isset($aUrl) && $aUrl != "") ? $aUrl : ""); ?>" onKeyUp="loadXMLDoc(this.value); update();" />
<span id="q"></span>
All I get is '0' how can I get the server response like 200 or 404 and the like ?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Tue Apr 12, 2016 10:22 am


Jason_C
New php-forum User
New php-forum User
Posts: 47
Joined: Sat Nov 18, 2006 7:32 am

Tue Apr 12, 2016 10:59 am


I am after the server response of someone else website, not mine.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Tue Apr 12, 2016 1:01 pm

Did you have a look at the page?

Jason_C
New php-forum User
New php-forum User
Posts: 47
Joined: Sat Nov 18, 2006 7:32 am

Tue Apr 12, 2016 1:24 pm

For example, I wish to get the server response of say...

http://www.google.com

or http://www.yahoo.com

and find out if I get redirected... and if so what is the new site i am taken to.

Post Reply