Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Bom.. Minha dúvida acho que é simples, criei um pequeno sistema de cadastro de 'url', o usuário coloca uma 'url' no formulário ele gera um nome aleatório e cadastra no banco de dados, minha dúvida é a seguinte:
Eu gostária que após o cadastro da URL exibisse ela encurtada na proxima tela, ou até mesmo na mesma tela,
segue abaixo meu sistema de cadastro de url;
<?php require_once('Connections/conexao.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO url (url_original, url_encurtada) VALUES (%s, %s)",
GetSQLValueString($_POST['url_original'], "text"),
GetSQLValueString($_POST['hiddenField'], "text"));
mysql_select_db($database_conexao, $conexao);
$Result1 = mysql_query($insertSQL, $conexao) or die(mysql_error());
// Página para onde sera redirecionado após o cadastro
$insertGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Encurtador de url</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Url Original</td>
<td><input type="text" name="url_original" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Encurtar" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="hidden" name="hiddenField" id="hiddenField" value="<?php
$tamanho = mt_rand(5,9);
$all_str = "abcdefghijlkmnopqrstuvxyzwABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$nome = "";
for ($i = 0;$i <= $tamanho;$i++){
$nome .= $all_str[mt_rand(0,61)];
}
echo $nome
?>" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
</body>
</html>
Obs: Essa parte é a parte em que o php gera um nome aleatório, eu coloquei ele em uma HiddenField.
<input type="hidden" name="hiddenField" id="hiddenField" value="<?php
$tamanho = mt_rand(5,9);
$all_str = "abcdefghijlkmnopqrstuvxyzwABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$nome = "";
for ($i = 0;$i <= $tamanho;$i++){
$nome .= $all_str[mt_rand(0,61)];
}
echo $nome
?>" />
No caso a única coisa que deve ser exibida após o cadastro da URL é esse nome aleatório,
Tabela do MYSQL
-- Estrutura da tabela url
--
CREATE TABLE IF NOT EXISTS `url` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url_original` varchar(500) NOT NULL,
`url_encurtada` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
No caso tem que exibir o valor da 'url_encurtada'
No caso quanto eu termino o cadastro da url ele ta redirecionando para um página qualquer,
o que eu realmente quero é o seguinte: Após a pessoa criar o URL
A Página aparece assim: Seu url encurtado é: http://localhost/URLDAPESSOA
sim, psé , em vez de você redirecionar isso para uma pagina qualquer redireicona para uma pagina exemplo para a pagina exibir.php
nisso você faz o redirecionamento para exibir.php?url=$encurtada ($encurtada seria o nome que gerou),
na pagina exibir.php
você faz oque falei
if (isset($_GET['url'])){
//Aqui dentro você verifica no banco de Dados se realmente existe o $_GET['url'] e depois da consulta se caso realmente existir você faz um if se existir exibe o echo como abaixo, se não diz, url Invalido.
echo "Sua URL encurtada é tal".$_SERVER['SERVER_NAME']."/".$_GET['url'];
}Cara eu não to conseguindo fazer o redirecionamento para o exibir.php?url=URLENCURTADA
$insertGoTo = "exibir.php?url=$nome";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
tentei assim mais mesmo assim não pego.
ele vai pra seguinte página: exibir.php?url=&
você pode fazer assim:
Apos o INSERT no sql, você faz um header né?, neste caso,
você pode fazer o header no tipo header("Location: index.php?url=Kajsdjabjdasb") //url seria o valor da encurtada
ai no arquivo faz