PHP - Checking If Page Exists
How can I check to see if a webpage exists with PHP.
for example http://www.google.com/ would be true and http://fakeurl32939232.com/filepage.html = false What function/syntax would I have to use? Similar TutorialsIf I do a mysql query from a database on a field that either has a number or is blank, how can I write php to check that. In other words, I am getting that value back into a variable called $isaresult and doing... Code: [Select] if ($isaresult !== '') { //value exists } else { //no value yet } It's that first line of code that doesn't seem to be working. Should I be using "null" or something instead? I'm not real experienced with checking if a value exists, so let me know the proper way to do it (since I assume there are multiple ways). Thanks! Hi folks, Working on something and I'm not sure how best to do it. It's a system whereby customers can login and see pdf files. Each customer has a unique reference number which is mirrored with a folder on the server, the folder name being the customer reference number. That's the first bit of checking. Then, within that customer folder there will be between 1 and X sub folders which are labelled by unique product reference numbers. Beyond that, each of the product reference folders will have a year folder with a folder for each month of the year going forward. Here's an example of the folder structu - Customer Number (e.g. 12345678) - Product Number (e.g. 456789) (could be more than one) - Year (e.g. 2011) (could be more than one) -Month (e.g. 01/02/03/04/05/06 etc) Essentially, I'm just unsure how to check if folders exist and display their contents if they do. Here's my code so far, which does the first part of checking to see if the product numbers exist, but not the folder checking (I've not activated the hyperlinks in this code yet). This doesn't go as far as years and months yet, just the product number: Code: [Select] $query = mysql_query("SELECT * FROM customers where id = '$id'"); while($rst = mysql_fetch_array($query)) { echo "$rst[company_name]"; } $query = mysql_query("SELECT * FROM sites where company_name = '$company'"); while($rst = mysql_fetch_array($query)) { echo "$rst[site]<br>"; if ($rst[product1] !== ""){ echo "- <a href='#'>$rst[product1]</a><br>"; } if ($rst[product1] == ""){ echo ""; } if ($rst[product2] !== ""){ echo "- <a href='#'>$rst[product2]</a><br>"; } if ($rst[product2] == ""){ echo ""; } if ($rst[product3] !== ""){ echo "- <a href='#'>$rst[product3]</a><br>"; } if ($rst[product3] == ""){ echo ""; } if ($rst[product4] !== ""){ echo "- <a href='#'>$rst[product4]</a><br>"; } if ($rst[product4] == ""){ echo ""; } if ($rst[product5] !== ""){ echo "- <a href='#'>$rst[product5]</a><br>"; } if ($rst[product5] == ""){ echo ""; } This is something that has intrigued me, that has only recently surfaced when viewing the forum. Which of the following methods of authenticating that a user exists would be better/faster/ect? Example 1 - Fetching Row Data <?PHP $username = 'LoserVille'; $password = 'password'; $myQuery = mysql_query("SELECT account_id FROM user_accounts WHERE username = '$username' AND password = '$password'"); $myQuery = mysql_fetch_assoc($myQuery); if($myQuery) { /*### User Exists ###*/ } else { /*### User Does Not Exist ###*/ } ?> Example 2 - Fetching Number of Results <?PHP $username = 'LoserVille'; $password = 'password'; $myQuery = mysql_query("SELECT account_id FROM user_accounts WHERE username = '$username' AND password = '$password'"); $myQuery = mysql_num_rows($myQuery); if($myQuery >= 1) { /*### User Exists ###*/ } else { /*### User Does Not Exist ###*/ } ?> Just looking for some insight, not really a problem Regards, PaulRyan. Hi Guys, I'm using the following code to write emails to a .txt file: // Write the email to file $leadsFile = "leads/emails.txt"; $fh = fopen($leadsFile, 'a') or die ("can't open leads file!"); $writeData = "$conEM\r\n"; fwrite($fh, $writeData); fclose($fh); which works great, but i can't figure out how to NOT write an email to the .txt file if it's already on it, i'm not using a database for a change i'm stumped lol thanks for any help guys Graham Hi, Im trying to generate a random 4- digit code, and then check if it is allready in the database. My current code is below, but im not sure how to make it loop again, if the code exists. Also, im not sure if this is the best way of doing this, maybe someone could suggest something better... This is basicly what im trying to do: 1. hash the code 2. check if it exits in the DB 3. if it does, re-generate, and repeat 2., if not, return it function getUniqueHash() { global $mysql_server, $mysql_user, $mysql_password; $code = substr(md5(uniqid(rand(), true)), 0, 4); mysql_connect($mysql_server, $mysql_user, $mysql_password); // connect // $sql = "SELECT * FROM `$mysql_db`.`short` WHERE `short` = '$code'"; $result = mysql_query($sql); $results = mysql_numrows($result); if($results > 0) { //GO BACK AND RE-GENERATE CODE AND REPEAT CHECK } else { return $code; } mysql_close(); } PasteBin: Quote http://paste.tgxn.net/?s=9 I have come across couple of similar answers for my question in this form, but could not solve my exact problem. Therefore I am posting this he I have an xml file as shown below: <?xml version="1.0" encoding="ISO-8859-1"?> <document> <user> <user_id>0121</user_id> <name>Tim</name> <file>0121.file</file> </user> <user> <user_id>0178</user_id> <name>Henry</name> <file>0178.file</file> </user> <user> <user_id>0786</user_id> <name>Martin</name> <file>0786.file</file> </user> <user> <user_id>1239</user_id> <name>Jan</name> <file>1239.file</file> </user> </document> I ask the user to input his user_id and post this user_id and perform a check on the entire xml file to check whether the entered user_id exists in the xml file or not. If exists then I go further if not echo a error message. Can anybody pull me out of this? Thanks Hi, I'm making a website that uses data stored in SQL. One of my pages allows the user to enter a new product into a product table. I was wondering if somebody could help me with some validation or Query that will not allow the user to enter a product name that already exists in the table? Code: [Select] $nameNew = $_POST['new]; $nameID = $_POST['newID]; $namePrice = $_POST['newPrice]; INSERT INTO products VALUES ($nameNew, $newID, $newPrice) $describeQuery = "SELECT ID, Name, Price FROM Products"; $results = sqlsrv_query($conn, $describeQuery); echo '<table border="1" BORDERCOLOR=Black>'; echo '<tr><th bgcolor = "LightBlue">Name</th><th bgcolor = "LightBlue" >ID</th> <th bgcolor = "LightBlue" >Price</th></tr>'; while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) { echo '<tr>'; echo '<td >' .$row['Name'].'</td>'; echo '<td>' .$row['ID'].'</td>'; echo '<td>' .$row['Price'].'</td>'; echo '</tr>'; } echo '</table>'; sqlsrv_close($conn); Thanks I am trying to allow a user to proceed to another page but only after a user has clicked a Facebook like button and a check has been done to check if the currently logged in user has liked the specific Facebook Page. How can I go about this? Thanks in advance Hi community,i creating a simple website and description i analyzed my code about 3days...2hours a day. if i get the point in my head.then its say Website url is in use..but not..ok maybe i give the code below. so if you can explain whats wrong.thanks Code: [Select] <?php include_once ('connect.php'); $url= mysql_real_escape_string($_POST['url']); $desc=mysql_real_escape_string($_POST['desc']); if ($id == "krauti") { if (empty($_POST['url']) && empty($_POST['desc'])) { die('<br><center>Both Empty!.<center/><br/>'); } if (empty($_POST['url'])) { die('<br><center>URL Empty!.<center/>'); } if (empty($_POST['desc'])) { die('<br><center>desc empty fill it!.<center/>'); } $url = htmlspecialchars( stripslashes( strip_tags($_POST['url'] ) ) ); $aprasymas = htmlspecialchars( trim($_POST['desc'] ) ); $query = mysql_query("SELECT * FROM linkai WHERE url = '$url' AND 'desc' = '$desc'"); if( mysql_num_rows( $query ) == 1 ) { "INSERT INTO linkai ". "(id, url, desc) ". "VALUES('','$_POST[url]','$_POST[desc]')"; echo "Yes!."; } else if (mysql_num_rows($query) == 0 ){ echo "No!!"; } } ?> Can someone explain why this code: if (mysql_result($data,0,$SQLLargeName) = " "){ $LargeImage = "#"; } else { $LargeImage = mysql_result($data,0,$SQLLargeName); } Gives me this error? Fatal error: Can't use function return value in write context in /home/worldw44/public_html/SQL.php on line 38 And ways round it? Thanks. Hi, I have a sign up form that generates a activation key and emails it to user and they cannot login until account has been activated etc. My problem is i am not sure how to in php generate a new activation key if the generated one has already been used. I know how to do the MySQL query to check if activation key already exists but not sure how to basically tell PHP that if the activation key already exists in DB then generate a new key until one is found that does not already exist. I gather i may need a loop but not sure. Could someone please help ? Like i say i know how to do the MySQL query just need some sample code to know how to do what i asked above. Thanks for anyhelp; Much Appreciated <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function popUp(URL,URL2) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL,'" + id + "', 'toolbar=0,scrollbars=1,location=0,top=10,left=10,statusbar=0,menubar=0,resizable=0,width=408,height=400');"); day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL2,'" + id + "', 'toolbar=0,scrollbars=1,location=0,top=10,left=450,statusbar=0,menubar=0,resizable=0,width=408,height=400');"); } // End --> </script>I have a map which shows sales people by location and if more then one sales person exists then I would need 2 pop up windows. This works great other than the fact that the areas with one sales person are getting a blank 2nd popup as a second salesperson does not exist. Any help would be appreciated. Thanks In Advance Edited by phpbeginner, 27 June 2014 - 05:52 AM. My error below returns (echos) that there is no data under the table sites to display. But, there is. Why is it doing this? class_l.php <?php class displaySites { function grabSites() { $query = mysql_query("SELECT title,description,votes,dates,comments,out,in FROM sites ORDER BY id DESC LIMIT 100"); if(mysql_num_rows($query) > 0) { while($fetch = mysql_fetch_assoc($query)) { echo " <div id='post-1' class='post'> <h2 class='title'><a href='#'>". $fetch['title'] ."</a></h2> <h3 class='date'>Submitted on ". $fetch['date'] ."</h3> <div class='entry'> <p>". nl2br(stripslashes($fetch['description'])) ."</p> </div> <p class='meta'><a href='#'>View</a></p> <div class='hr'> </div> </div> "; } } else { echo " <div id='post-1' class='post'> <h2 class='title'><a href='#'>Oh NOEZ!</a></h2> <h3 class='date'>". date("M-d-Y") ."</h3> <div class='entry'> <p>There are currently no sites to display!</p> </div> <p class='meta'>View</p> <div class='hr'> <hr /> </div> </div> "; } } } class register { function createAccount() { $username = mysql_real_escape_string($_POST['username']); $password = md5(sha1(sha1($_POST['password']))); $name = mysql_real_escape_string($_POST['title']); $description = mysql_real_escape_string($_POST['description']); if(isset($username) && isset($password) && isset($name) && isset($description)) { if(!$username || !$password || !$name || !$description) { echo '<table border="0"><form action="register.php" method="POST"> <tr><td>Username</td><td><input type="text" name="username" maxlength="20"></td></tr> <tr><td>Password</td><td><input type="text" name="password" maxlength="30"></td></tr> <tr><td>Site Name</td><td><input type="text" name="title" maxlength="25"></td></tr> <tr><td>Site Description</td><td><textarea cols="40" rows="18" name="description" maxlength="750"></textarea></td></tr> <tr><td>Finish!</td><td><input type="submit"></td></tr> </form></table>'; } else { $query = mysql_query("SELECT * FROM sites WHERE username = '$username' LIMIT 1"); if(mysql_num_rows($query) > 0) { echo "Sorry, an account with this username already exists."; } else { $date = date("M-d-Y"); $ip = $_SERVER['REMOTE_ADDR']; //create their account mysql_query("INSERT INTO sites VALUES (null, '$username', '$password', '$name', '$description', 0, 0, 0, '$date', '$ip', 0)"); echo "You have successfully registered your site on NovaTops! <a href='login.php'>Login</a>"; } } } else { echo "One or more of the variables are not set."; } } } class login { function verifyLogin() { $username = mysql_real_escape_string($_POST['username']); $password = md5(sha1(sha1($_POST['password']))); if(isset($username) && isset($password)) { if(!$username || !$password) { echo "<table><form action='login.php' method='POST'> <tr><td>Username</td><td><input type='text' name='username' maxlength='20'></td></tr> <tr><td>Password</td><td><input type='password' name='password' maxlength='30'></td></tr> <tr><td>Finished?</td><td><input type='submit'></td></tr> </form></table>"; } else { $query = mysql_query("SELECT * FROM sites WHERE username = '$username' AND password = '$password'LIMIT 1"); if(mysql_num_rows($query) > 0) { echo "Congratulations! You have successfully logged in! <a href='index.php'>Home</a>"; $_SESSION['user'] = $username; } else { echo "It seems you've typed in the wrong username and/or password. <a href='login.php'>Try Again</a>"; } } } else { echo "One or more of the variables aren't set."; } } } ?> index.php <?php include_once('includes/config.php'); include_once('class_l.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title><?php echo $title; ?></title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="styles.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div class="top_text"><h1><a href="#"><?php echo $site_name; ?></a></h1> <h2><a href="#"><?php echo $slogan; ?></a></h2></div> <div id="content"> <div id="blog"> <div id="post-1" class="post"> <h2 class="title"><a href="#">Welcome to NovaTops!</a></h2> <h3 class="date">Created on April 06, 2011</h3> <div class="entry"> <p>Hello there! If your viewing this message right now, your on the awesome site called NovaTops! NovaTops allows you to register your site in our database, and then the site's script automatically starts to rank your site. The higher your rank, the more likely your site's going to get a ton of views!</p> </div> <div class="hr"> <hr /> </div> </div> <!-- end #post-1 --> <?php $displaySites = new displaySites(); $displaySites->grabSites(); ?> </div> <!-- end #blog --> <?php include_once('includes/navigation.php'); ?> <!-- end #sidebar --> <div style="clear: both; height: 1px;"></div> </div> <!-- end #content --> <div id="footer"> <p>Copyright © 2011. Design by <a href="http://www.flashtemplatesdesign.com/" title="Free Flash Templates">Free Flash Templates</a></p> <p><a href="#">Privacy Policy</a> | <a href="#">Terms of Use</a> | <a href="http://validator.w3.org/check/referer" title="This page validates as XHTML 1.0 Transitional"><abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a> | <a href="http://jigsaw.w3.org/css-validator/check/referer" title="This page validates as CSS"><abbr title="Cascading Style Sheets">CSS</abbr></a></p> </div> </div> <!-- end #wrapper --> </body> </html> Hi all, I have the following mysql delete query that essentially deletes a row from two seperate tables "messages" and "messages2" where the id is equal to that in both of the tables. The problem I am getting (for obvious reasons) is that it will only delete from the table, if it indeed exists in both tables. Here is the query: Code: [Select] DELETE FROM messages,messages2 USING messages,messages2 WHERE messages.id = messages2.id AND messages.id = ?; How would I go about setting it so that the query always deletes from table "messages" and only deletes from "messages2" when a corresponding id exists? I think it would involve an IF EXISTS clause, but I am not sure on this, or indeed how to implement it. Thanks in advance guys Dan This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=328831.0 Maybe I am over thinking this, but I would like to see if their is another value after the current value without moving the internal array pointer. What can I do? I apologize in advance for any misuse of terminology. I find that I am rather ignorant in such things. I'm working on a WordPress theme. Specifically on the part that shows the "next: link-to-next-post" and "previous: link-to-prev-post" links. Here is the code that I have: Code: [Select] <?php $nextvar=next_post_link('%link'); $prevvar=previous_post_link('%link'); ?> <?php if (isset($nextvar)) { ?> <h3>Next</h3> <p><?php next_post_link('%link'); ?></p> <? } ?> <?php if (isset($prevvar)) { ?> <h3>Previous</h3> <p><?php previous_post_link('%link'); ?></p> <? } ?> The first part intends to check if the links exist. And this works fine. However, the only output I'm getting are the values of $nextvar and $prevvar. The headers are nonexistent. What's wrong? Any help is much appreciated, and I thank you in advance. Hello, I want to check when adding a new user, that if there are 2 records that has the same recruiter id, it's should give an error, if less, than it makes him complete the registration Also check if the National ID number is not assigned to another user, so if there is a record that has it, it should give and error, if it's unavailable, it should let him complete the registration here is my code register.html Code: [Select] <html> <form action="post.php" method="post"> <table> <tr><td>First Name: <input type="text" name="fname" /></td></tr> <tr><td>Middle Name: <input type="text" name="mname" /></td></tr> <tr><td>Last Name: <input type="text" name="lname" /></td></tr> <tr><td>Mobile: <input type="text" name="mobile" /></td></tr> <tr><td>Telephone: <input type="text" name="tel" /></td></tr> <tr><td>Address: <input type="text" name="address" /></td></tr> <tr><td>Job Title: <input type="text" name="job" /></td></tr> <tr><td>Company: <input type="text" name="company" /></td></tr> <tr><td>National ID Number: <input type="text" name="nid" /></td></tr> <tr><td>Recruiter ID: <input type="text" name="recruiteris" /></td></tr> <tr><td>Email: <input type="text" name="email" /></td></tr> <tr><td>Password: <input type="password" name="password" /></td></tr> <tr><td>Type: <input type="text" name="type" /></td></tr> <tr><td><input type="submit" /></td></tr> </table> </form></html> post.php Code: [Select] <?php $sql="INSERT INTO users (id,fname, mname, lname, mobile, tel, address, job, company, nid, recruiteris, email, password, type) VALUES ('','$_POST[fname]','$_POST[mname]','$_POST[lname]','$_POST[mobile]','$_POST[tel]','$_POST[address]','$_POST[job]','$_POST[company]','$_POST[nid]','$_POST[recruiteris]','$_POST[email]','$_POST[password]','$_POST[type]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; echo "<br />"; echo "<a href='http://waw-eg.com/admin/register.html'>Add More Client? Click Here</a><br/>"; echo "<a href='http://waw-eg.com/admin/Users.php'>View Users Click Here</a>"; mysql_close($con) ?> The Script:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <input type="text" name="hashtags" /> <input type="submit" name="submit" /> </form> <?php if(isset($_POST['submit'])){ $hashtags = explode(", ", $_POST['hashtags']); // Prints e.g.: Array ( [0] => #tag1 [1] => #tag2 [2] => #tag3 [3] => #tag4 ) print_r($hashtags); } ?>This gets inserted into the input field: #tag1, #tag2, #tag3, #tag4I am looking to check if any of the hashtags inside the array already exist in the database, if it does not exists it should create the new ones in the table. I know how to do this if all do not exists in the array and then it goes over to the MySQL query and inserts all of them. My Question Is: How to insert only the ones which do not exists out of the array, so the ones which do exists do not get inserted again into the table? Edited by glassfish, 14 October 2014 - 10:15 AM. Looking for the best method to conditionally strip leading zeros for the following situation: $a = array(01, 02, 03, ... 10, 11); $a < 10 ? $a = ? : ''; For the following result: 1, 2, 3, ... 10, 11 Is there a better method than explode? |