Okay so I'm trying to redirect a user back to subto.me.com if subto.me/args doesn't exist I have this code
if(file_exists($BASEPATH."partials/".$request['args'][0].".php")) {
require($BASEPATH."partials/".$request['args'][0].".php");
} else {
header("Location: http://www.youtube.com/subscription_center?add_user=".$request['args'][0]);
}
If the youtube user doesn't exist, it will say so using a link like this https://gdata.youtube.com/feeds/api/users/fdsafsdfasdfasd. How would I incooperate it if source code equals "User not found"
I have a solution for that:
$yuser = "http://www.youtube.com/user/xxxx";
$yt_headers = #get_headers($yuser);
if($yt_headers[0] == 'HTTP/1.1 404 Not Found')
{ echo "Youtube user dosen't exist"; }
else { echo "Youtube user exists"; }
this can be used as a function to make the process more pretty!
Related
I'm new to PHP and am even more of a beginner when it comes to sessions. I have my index.php page, which is where users can register and login. The forms are posting to validate.php and loginvalidate.php pages, respectively for registering and logging in.
I have these errors on index.php when I load it:
1) Notice: Undefined index: registered
2) Notice: Undefined index: neverused
I have tried modifying my text in many ways but I never got to solve the errors.
Index.php
<?php
if ($_SESSION['registered'] != NULL){
echo $_SESSION['registered'];
}
if ($_SESSION['badlogin'] != NULL){
echo $_SESSION['badlogin'];
}
if ($_SESSION['neverused'] != NULL) {
echo $_SESSION['neverused'];
}
?>
Validate.php (after submitting register form)
if (mysqli_num_rows($result) > 0) { //IF THERE IS A PASSWORD FOR THAT EMAIL IN DATABASE
$_SESSION['registered'] = "Email is already registered.";
mysqli_close($db_handle);
header('Location: index.php');
exit();
}
Loginvalidate.php (after submitting login form)
if ($numrows!=0) //IF THERE IS A PASSWORD FOR THAT EMAIL IN THE DATABASE
{
if ($row['password'] == $password) { //IF THE PASSWORD MATCHES USER INPUT
header('Location: homepage.php');
echo "lol";
exit();
}
else{
$_SESSION['badlogin'] = "Email/password combination not valid.";
mysqli_close($db_handle);
header('Location: index.php');
exit();
}
}
else { //THERE IS NO PASSWORD FOR THAT EMAIL, SO THAT EMAIL IS NOT REGISTERED
$_SESSION['neverused'] = "Email is not registered.";
mysqli_close($db_handle);
header('Location: index.php');
exit();
}
Okay so my script does what it is intended to do. The only thing that I can't solve is these session errors. Do you see any misuse of sessions? Of course, I have started the sessions in all of my .php files.
Also, note that I am aware that there is no protection from hackers. This is only for a future prototype that won't contain any important data.
The reason for these errors is that you're trying to read an array key that doesn't exist. The isset() function is there so you can test for this. Something like the following for each element will work a treat; there's no need for null checks as you never assign null to an element:
// check that the 'registered' key exists
if (isset($_SESSION['registered'])) {
// it does; output the message
echo $_SESSION['registered'];
// remove the key so we don't keep outputting the message
unset($_SESSION['registered']);
}
You could also use it in a loop:
$keys = array('registered', 'badlogin', 'neverused');
//iterate over the keys to test
foreach($keys as $key) {
// test if $key exists in the $_SESSION global array
if (isset($_SESSION[$key])) {
// it does; output the value
echo $_SESSION[$key];
// remove the key so we don't keep outputting the message
unset($_SESSION[$key]);
}
}
If you're getting undefined index errors, you might try making sure that your indexes are set before you try comparing the values. See the documentation for the isset function here:
http://php.net/manual/en/function.isset.php
if (isset($_SESSION['registered']))
if ($_SESSION['registered'] != NULL){
echo $_SESSION['registered'];
}
}
if (isset($_SESSION['badlogin']))
if ($_SESSION['badlogin'] != NULL){
echo $_SESSION['badlogin'];
}
}
if (isset($_SESSION['neverused']))
if ($_SESSION['neverused'] != NULL) {
echo $_SESSION['neverused'];
}
}
I'm trying to figure out if a Instagram User exists or not. If the user site doesn't exist, it should give me a '404 Not Found' in the headers, but i'm always getting a '301 Moved Permanently', even if the user doesn't exist!
My browser gives me a 404:
https://i.imgur.com/to38Sb2.png
Can u help me? This is my code:
<?php
$url = "https://www.instagram.com/asdsdfsvxd"; //This user doesn't exist
echo $url;
$file_headers = #get_headers($url);
echo $file_headers[0];
if($file_headers[0] !== 'HTTP/1.1 404 Not Found') {
echo "exists";
} else {
echo "not exists";
}
?>
Solution:
Dont forget the trailing / (slash).
$url = "https://www.instagram.com/asdsdfsvxd/";
Output:
$ php test.php
https://www.instagram.com/asdsdfsvxd/HTTP/1.1 404 Not Foundnot exists
hello im looking to show a message to users when they come on my page from different sites https sites or http can someone help me to redirect users coming from all sites example if they have referral different sites with https or http referrals, this is working only from http referrals
<?php
$pos = (strpos($_SERVER['HTTP_REFERER'], 'http'));
if($pos===false)
die('<h1>Restricted access</h1>');
else
{echo '<h1>It Works</h1>';}
?>
You can use this to detect whether the referral URL was http://
<?php
if(strstr($_SERVER['HTTP_REFERER'], 'http://')) {
die('<h1>Restricted access</h1>');
} else {
echo '<h1>It Works</h1>';
}
?>
If you just want to check whether the user clicked a link from another site to arrive at yours then you can check whether $_SERVER['HTTP_REFERER'] exists:
You can use this to detect whether the referral URL was http://
<?php
if($_SERVER['HTTP_REFERER']) {
die('<h1>Restricted access</h1>');
} else {
echo '<h1>It Works</h1>';
}
?>
generally regex should be used for such kind of problem ... the below should work for your problem ...
if (preg_match('/http(s)?/',$_SERVER['HTTP_REFERER'])) {
echo 'redirect now';
} else {
echo 'stay here';
}
I work on two servers (A and B, and I need to show on A some images from B. The problem is I need to test if the image exists (else I show only the name of the client).
When I test in local, this works:
if (file_exists(http://www.example.com/images/3.jpg)) {
// I show the file here
} else {
// I just show the name
}
But, that's don't works now. Help please !
You can't use file_exists for a checking remote files.
Use get_headers function and check, if the file returns 200 or not.
$file_headers = get_headers('http://www.example.com/images/3.jpg');
if ($file_headers[0] == 'HTTP/1.1 200 OK') {
echo "The file exists";
} else {
echo "The file doesn't exist";
}
try to use this
$array= get_headers("http://images.visitcanberra.com.au/images/canberra_hero_ima.jpg");
echo $h= $array[0];
it will return HTTP/1.1 404 Not Found if not exists and HTTP/1.1 200 OK if exists
function is_file_on_url_exists($url)
{
if (#file_get_contents($url, 0, NULL, 0, 1))
{
return true;
}
return false;
}
try this one out !!
I have a page where the user can enter what he is selling and be redirected to that page. I know the mysql LIKE '%%' function but I am not using database. If the case matches, I just would like to redirect to that page. How is that possible? whether it is javascript or php?
For examples: if the user enters "laptop" then he/she will be redirected to laptop.php(I have the pages). How can I do that?
The form structure:
<form class="sellcont" action="" method="post">
<input type="text" name="selling" >
<input type="submit" name="submit" value="Next" class="myButton">
</form>
<?php if(isset($_POST['submit'])){
$sell = $_POST['selling'];
if(!empty($_POST['selling'])){
//This is where I am lost.
}
}
After Comments
You use php header to redirect and check the input ($_POST['selling']) with in_array if its a good input.
$goodinput = array("laptop", "pc", "mac", "cellphone");
if(!empty($_POST['selling'])){
if (in_array($_POST['selling'],$goodinput)){ //checks if user input is a good input
header('Location: http://www.yoururl.com/'.$_POST['selling'].'php');
exit();
} else {
header('Location: http://www.yoururl.com/wedonthavethatpage.php');
exit();
}
}
Suggestion:
Instead of creating individual pages e.g.: laptop.php you store similar values in a database and create dynamic pages.
PHP code should be like this:
<?php if(isset($_POST['submit'])){
$sell = $_POST['selling'];
if(!empty($_POST['selling'])){
header('location: ./'.$_POST['selling'].'.php');
}
else {
header('location: ./index.php'); //index.php is PHP files on which user enter selling item
}
}
?>
You could also use the code in this answer:
How can I check if a URL exists via PHP?
to see if the what the user typed in is available. If so, do the redirect, if not, send it to an alternate page.
E.g.
if(!empty($_POST['selling'])){
$file = 'http://www.yourdomain.com/'.$_POST['selling'].'.php';
$file_headers = #get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
if ($exists) {
header(sprintf('Location: %s', $file);
}
else {
header('Location: http://www.yourdomain.com/other_page.php');
}
exit();
}