I want to remove things after .com - php

Hello I found a way to remove www https but now I need to remove anything after ".com" www.something.com/something/something I want to remove */something/something~ from the URL.
<form method="post" action="">
<label>URL adresa:</label><input type="text" placeholder= "URL" name="url" required /><br>
<label>Titulek odkazu:</label><input type="text" placeholder= "Titulek" name="titulek" required/><br>
<label>Otevření nového okna ANO</label><input type="checkbox" name="choice1" /><br>
<label>Barva odkazu:</label><input type="color" name="color" /><br>
<input type="submit" name="submit" />
</form>
<?php
$zakaz = array('www.', 'https://', 'http://' );
if(isset($_POST['url']) AND isset($_POST['titulek']) AND isset($_POST['color']) AND !isset($_POST['choice1'])){
$url = $_POST['url'];
$titulek = $_POST['titulek'];
$color = $_POST['color'];
echo "<a href='" . $url . "'title ='" . $titulek . "'style=' color:" . $color . "; text-decoration: none ;'>" . str_replace($zakaz, '', $url) . "</a>";
}elseif(isset($_POST['url']) AND isset($_POST['titulek']) AND isset($_POST['color']) AND isset($_POST['choice1'])){
$url = $_POST['url'];
$titulek = $_POST['titulek'];
$color = $_POST['color'];
echo "<a href='" . $url . "'title ='" . $titulek . "' target= '_blank' style=' color:" . $color . "; text-decoration: none ;'>" . str_replace($zakaz,'',$url ) . "</a>";
}
?>

You can use PHP's explode().
You can use:
$website = explode("/",$fullwebsite)[0]; //something.com

Related

Issues with Inserting array hidden form values into the database [duplicate]

I have seen a ton of people like me posting about this issue on this forum.
I have not been able to resolve my own issue from any of those examples.
For instance, this code below is from a page called review.php:
<?php
error_reporting(E_ALL);
echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>";
if(isset($_POST['employeename']))
$employeename = $_POST['employeename'];
if(isset($_POST['email']))
$email = $_POST['email'];
if(isset($_POST['ttitle']))
$ttitle = $_POST['ttitle'];
$rowIDs = $_POST['rowIDs'];
$row2IDs = $_POST['row2IDs'];
echo $employeename .'<br>';
echo $ttitle .'<br> <hr width=400 align=left>';
$rowIDs = $_POST['rowIDs'];
foreach ($rowIDs as $id) {
$sourcename = $_POST['sourcename' . $id];
$sourceaddress = $_POST['sourceaddress' . $id];
$income = $_POST['income' . $id];
echo 'Name: '. $sourcename . '<br />';
echo 'Address: '. $sourceaddress . '<br />';
echo 'Income: '. $income . '<br /><br>';
}
foreach ($row2IDs as $id) {
$spousename = $_POST['spousename' . $id];
$spouseAddress = $_POST['spouseAddress' . $id];
$spouseIncome = $_POST['spouseIncome' . $id];
echo 'Name: '. $spousename . '<br />';
echo 'Address: '. $spouseAddress . '<br />';
echo 'spouseIncome: '. $spouseIncome . '<br /><br>';
echo 'Your email: '. $email . '<br /><br>';
}
?>
<body>
<form action='final.php' method = 'POST'>
<input type="hidden" name="employeename" value="<?php echo $employeename; ?>">
<input type="hidden" name="ttitle" value="<?php echo $ttitle; ?>">
<input type="hidden" name="sourcename[]" value="<?php echo $_POST['sourcename' . $id]; ?>">
<input type="hidden" name="sourceaddress[]" value="<?php echo $_POST['sourceaddress' . $id]; ?>">
<input type="hidden" name="income[]" value="<?php echo $_POST['income' . $id]; ?>">
<input type="hidden" name="spousename[]" value="<?php echo $_POST['spousename' . $id]; ?>">
<input type="hidden" name="spouseAddress[]" value="<?php echo $_POST['spouseAddress' . $id]; ?>">
<input type="hidden" name="spouseIncome[]" value="<?php echo $_POST['spouseIncome' . $id]; ?>">
Return to correct changes <input type="submit" value="submit" />
</form>
</body>
When I run it, I get:
Notice: Array to string conversion in C:\xampp\htdocs\folder\forms\final.php on line 70
The error points to this line:
if( mysqli_stmt_execute($sth) ) {...
which is a part of the following insert statement:
$sql = 'INSERT INTO `mydb`.`wp_mytable` ( `employeeID`'
. ', `sourcename`, `sourceaddress`, `income`,`spousename`,`spouseAddress`,`spouseincome` )'
. ' VALUES ( ? , ? , ? , ? , ? , ? , ? )';
if( $sth = mysqli_prepare($conn,$sql) ) {
mysqli_stmt_bind_param($sth,'sssssss'
,$last_id
,$_POST["sourcename"]
,$_POST["sourceaddress"]
,$_POST["income"]
,$_POST["spousename"]
,$_POST["spouseAddress"]
,$_POST["spouseIncome"]
);
I also know that I need to do a FOR loop on these fields like this one below that I am passing as an array in hidden form.
<input type="hidden" name="sourcename[]" value="<?php echo $_POST['sourcename' . $id]; ?>">
How do incorporate a foreach loop on these hidden form fields on review.php so that users can store as many rows as possible without these errors getting in the way?
Thank you

Save color using cookies

Hello i want to save with cookie my color from input and if user refresh page color will be saved and ready to next use.Cookies must be set only for 1 hour and if expired cookies will be deleted.Sorry for my english and thanks for all help
<table>
<form action='' method='post'>
<tr><td><label>URL adresa: </label></td><td><input type='text' placeholder='napr.google.com' name='url' /></td></tr>
<tr><td><label>Titulek: </label></td><td><input type='text' placeholder='napr.google' name='title' /></td></tr>
<tr><td><label>Vyberte barvu: </label></td><td><input type='color' name='color' /></td></tr>
<tr><td><label>Otevřít v novém okně </label></td><td><input type='checkbox' name='window' />
<tr><td></td><td><input type='submit' name='submit' /></td></tr>
</form>
<?php
if (isset($_POST['submit']))
{
if (!empty($_POST['url']))
{
if (!empty($_POST['title']))
{
$url = $_POST['url'];
$title = $_POST['title'];
$color = $_POST['color'];
$explode1 = explode("/",$url);
$explode2 = explode(".",$explode1[2]);
setcookie("color", $color, time() + 3600, '/');
if (isset($_POST['window']))
{
setcookie("window", $_POST['window'], time()+3600, '/');
echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
} elseif (isset($_COOKIE['window'])) {
echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
} else {
echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . ">" . $explode2[1] . "." . $explode2[2] . "</a>";
}
}
}
}
?>
You never save $color to your cookie:
if (isset($_POST['window'])) {
setcookie("window", $color, time() + 3600, '/');
echo "<a style='color:" . $color . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
}
Or if $color needs to be saved separately:
setcookie("color", $color, time() + 3600, '/');
Personally, I would save it to a Session like so:
session_start();
$_SESSION['color'] = $color;
Hope that helps.
Edit:
if (isset($_POST['submit']) && (!empty($_POST['url']) && (!empty($_POST['title'])) {
$url = $_POST['url'];
$title = $_POST['title'];
$color = (($_POST['color'] == $_COOKIE['color'])?$_COOKIE['color']:$_POST['color']);
$explode1 = explode("/",$url);
$explode2 = explode(".",$explode1[2]);
setcookie("color", $color, time() + 3600, '/');
if (isset($_POST['window'])) {
setcookie("window", $_POST['window'], time()+3600, '/');
echo "<a style='color: $color' href='$url' target='_blank'>$explode2[1]}.{$explode2[2]}</a>";
} else {
echo "<a style='color: $color' href='$url'>{$explode2[1]}.{$explode2[2]}</a>";
}
}

trying to get PHP to post string to a file

I'm trying to use PHP to send the values of a form, in the format of a list item which contains an image and a caption to be added to a textfile which is then read by another page. The image upload part works fine but the output of the php looks like
<li><img alt= " " src=" " > </li>
when it should be more like <li><img src="img/comics/sa005.png">Comic 5 - </li>
<?php
//get item details
$title = $_POST['title'];
$alt = $_POST['alt'];
$caption = $_POST['caption'];
$location = $_POST['location'];
//file location
$file = 'listitems.txt';
// The list item to add to the file
$string = ("\n <li>" . $title . "<img alt= \" " . $alt . " \" src=\" " . $location . " \" > " . $caption . "</li>");
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $string, FILE_APPEND | LOCK_EX);
echo "string = ";
echo $string;
?>
<form action="appendlist.php" method="post">
<input name="title" type="textbox" placeholder="Title" class="form-control" ><br><br>
<input name="alt" type="textbox" placeholder="Alt text" class="form-control" ><br><br>
<input name="caption" type="textbox" placeholder="Image caption" class="form-control" ><br><br>
<input name="location" type="textbox" placeholder="File location" class="form-control" >
<br>
<input type="submit" value="Add Image" name="submit">
</form>
Your code is also right. but you should have remove extra blank space. if that is not work then another problem.
instead of trying to escape the double quotes, try to just use single quotes where you need a string encapsulated
$string = ("\n <li>" . $title . "<img alt='" . $alt . "' src='" . $location . "' > " . $caption . "</li>");

PHP and mySQL "UPDATE" doesn't actually update

So me and my friend came to a conclusion that it's the $_email variable that screws everything up. As long as it's hard coded in, it works. But as soon as it's left as a $_email everywhere, it doesn't. The message goes through as "updated" but it doesn't update.
require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="text" name="firstName" id="firstName" value="' . $_row['firstName'] . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_row['lastName'] . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_row['email'] . '" /><br />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
//$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
/*echo '<form class="update" method="post" action="MT_vjones_updateRecord.php">';
echo '<input type="text" name="firstName" id="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_email . '" /><br />';
echo '</form>';*/
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="hidden" name="firstName" value="' . $_firstName . '" />';
echo '<input type="hidden" name="lastName" value="' . $_lastName . '" />';
echo '<input type="hidden" name="email" value="*testBACK2FUN#test.com*" />';
}
echo '<p><< Back to the Admin Page</p>';
As you can see, we put in the email address in there for testing purposes...
check the id matches what you are intending to update.
To be sure print the $_id and $_email prior to the update and after.
#user710502: You don't need to segregate quotes with double-quotes in PHP. It reads it anyway, the only time you might bother is if you are reading from an array
eg:
"UPATE midterm SET email='".$POST['email']."'"
$_query = "UPDATE midterm " .
"SET email = '$_email' WHERE id = '$_id'" ;
should be
$_query = "UPDATE midterm " .
"SET email = $_email".
"WHERE id = $_id " ;
Reason is you are using $_ before variable which is not a valid variable declaration.
Because $_ is reserved for SUPER GLOBAL in php (i.e $_SESSION,$_SERVER,$_POST,$_GET,$_COOKIE etc).
if its not a issue for you then you need to concat your variable as below.
$_query = "UPDATE midterm SET email = '".$_email."' WHERE id = '".$_id."'" ;
SOLVED! Form issues.
SHOULD BE:
<?php
require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="hidden" name="firstName" id="firstName" value="' . $_row['firstName'] . '" />';
echo '<input type="hidden" name="lastName" id="lastName" value="' . $_row['lastName'] . '" />';
echo '<input type="hidden" name="email" id="email" value="' . $_row['email'] . '" />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="text" name="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" value="' . $_email . '" />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
}
echo '<p><< Back to the Admin Page</p>';
?>

Implementing Gravatar into custom Commenting System

I'm in the process of coding my very first blog. With the help of various tutorials, and other forums I have managed to gather a semi-working code.
Right now I have a code that takes and displays the comment, but the problem is I wish to display Gravatars beside each comment. I was just wondering how exactly I would go about implementing the code that they provided on their website.
Here is my current comment form:
<?php
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<hr />");
print("<p>" . stripslashes($row['comment']) . "</p>");
printf("<p>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
printf("<hr />");
}
?>
<form method="post" action="process.php">
<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />
<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">
<strong><label for="name">Name:</label></strong> <input type="text" name="name" id="name" size="25" /><br />
<strong><label for="email">E-mail:</label></strong> <input type="text" name="email" id="email" size="25" /><br />
<strong><label for="url">URL:</label></strong> <input type="text" name="url" id="url" size="25" value="http://" /><br />
<strong><label for="comment">Comment:</label></strong><br />
<textarea cols="25" rows="5" name="comment" id="comment"></textarea></p>
<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>
</form>
If you wish for me to post the php that processes each comment here as well just ask below.
My code now:
<?php
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
$url = 'http://www.gravatar.com/avatar/';
$url .= md5( strtolower( trim( $email ) ) );
$url .= "?s=$s&d=$d&r=$r";
if ( $img ) {
$url = '<img src="' . $url . '"';
foreach ( $atts as $key => $val )
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
return $url;
}
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<hr />");
print("<p>" . stripslashes($row['comment']) . "</p>");
printf("<p>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
echo $imagetag = "<img src='" . get_gravatar($email) . "' />";
printf("<hr />");
}
?>
You want an image tag whose src comes from the gravatar function.
Something like:
$imagetag = "<img src='" . get_gravatar($email_address) . ' />";
(You'll need to echo this variable where you want it to display.)
The only required parameter of the get_gravatar function is the email address, so just pass this to get_gravatar and you get the url of the gravatar image.
You also may use the Libravatar service that provides a gravatar-compatible but open source and federated alternative.
It has a nice PHP library - Services_Libravatar - that's easy to use:
<?php
require_once 'Services/Libravatar.php';
$sla = new Services_Libravatar();
$imgUrl = $sla->getUrl('foo#example.org');

Categories